#1 26 Aug 2006 06:03

NDog
Member
From: New Zealand
Registered: 31 May 2006
Posts: 119
Website

FOR /F Long File Names in batch files

I have been FOR /F for a long time, one thing I haven't been able to do is make LFN (Long File Names) work with the FOR /F command.

I have used a CD /D in my batch files before using FOR /F command, but now I need to use LFN inside a batch file but I still can't work out how the FOR command works, all the for commands I use have been copied straight from ss64.com and I have somehow managed to make them work.

What I'm trying to do

CD\
SET LOCALKEYSHARE=C:\keys
SET GAME=Quake III Arena
FOR /F "TOKENS=*" %%G IN ('DIR/B %LOCALKEYSHARE%\%game%.*') DO ECHO=%%G

In the LOCALKEYSHARE (c:\long folder name\another long folder)folder is a file called Quake III Arena.1. I am simply trying to make the FOR /F find files called Quake III Arena.* and make it %G if you understand. I tried ('DIR/B ^%LOCALKEYSHARE%\%game%.*^') but that didn't work either.

Thanks for the help



Other stuff I have done, which kinda relates to my lack of knowledge and improvisation.

Example One

:REGIMPORT
SET FILE=DONE
FOR /F "TOKENS=*" %%G IN ('DIR/B /A-D ^%LOCALKEYSHARE%\*.REG^') DO SET FILE=%%G
SET FILE="%FILE%"
IF "DONE"==%FILE% GOTO UPDATING
SET FILE=%FILE:~1,-1%
REG IMPORT "%LOCALKEYSHARE%\%FILE%" 2>nul >nul
DEL "%LOCALKEYSHARE%\%FILE%"
GOTO REGIMPORT

:UPDATING

Example Two

:UPDATING2
CD /D "%GAMEDIR%\SteamApps"
MD safe
FOR /F "TOKENS=*" %%G IN ('DIR/B /AD /O-D') DO SET FOLDER=%%G
REN "%FOLDER%" "%USERKEY%"

:USERFOLDER
FOR /F "TOKENS=*" %%G IN ('DIR/B /AD /OD') DO SET FOLDER=%%G
SET FOLDER="%FOLDER%"
IF "%USERKEY%"==%FOLDER% GOTO GAMEFILES
RD %FOLDER%
GOTO USERFOLDER

:GAMEFILES

In example One you see I have to put brackets around the variables to compare them with other variables, then I have to remove the brackets using the %X:~1,-1%. This is okay for me because it works at least. However I want to be able to use the FOR /F "TOKENS=*" %%G IN ('DIR/B /AD /OD') DO SET FOLDER=%%G from another path eg c:.

In the example Two "%GAMEDIR%\SteamApps" equals C:\Program Files\steam\SteamApps, so I need to use CD /D "%GAMEDIR%\SteamApps" before I use the FOR /F "TOKENS=*" %%G IN ('DIR/B /AD /OD') DO SET FOLDER=%%G command.


cmd, vbs, ps, bash
autoit, python, swift

Offline

#2 26 Aug 2006 06:50

//[T.0.P]//
Member
Registered: 12 Aug 2006
Posts: 50

Re: FOR /F Long File Names in batch files

double quotes and brackets are used for matching values. So if you have quotes in your values and then barricade the value when comparing with more quotes ""value""=="value", they will not equal the same value. As a matter a fact I believe you will get errors. Adding to that note, I never use double quotes in my variables' values.

As far as trying to get to long file names really depends on what exactly you're trying to do. And from what I can see all you're doing is using the DIR to get some file listings in bare format. All you have to do is just double quote the path. in a FOR /F loop, you can only use one set of double quotes.

I also have made a batch script that converts longfile names to their short filenames, but only for directories. And all this information about Long Files Names is on this thread

Good Luck!

Offline

#3 26 Aug 2006 12:06

NDog
Member
From: New Zealand
Registered: 31 May 2006
Posts: 119
Website

Re: FOR /F Long File Names in batch files

All I am trying to do is get the extension of a file in a path, but I didn't know how to do it. This appears to work now.
BTW I read your site, and it appears you are a game creator. I am working at New Zealands biggest internet cafe, and I am in charge of the games system now, I have made scripts to make a game run with 0 user interaction which is cool. I have gotten steam, battlefield vietnam, and over 30 more i cant be bothered listing, but I am making a 'generic' script which is just an engine, so at the start of the batch file you just have to type the game folder game name game cd keys cd image and all the network paths etc, and then the script will just sort it from there. Usually though making a game script is really easy, mount the virtual disc import a registry script run the game + parameters. So we are both gaming enthuiasts, well that is great I am glad to have your help. Cheers

cd\
set gamepath=C:\Program Files\Quake 3
set game=Quake III Arena
FOR /F "TOKENS=*" %%G IN ('DIR/B "%gamepath%\%game%.*"') DO SET X=%%~xG
SET X=%X:~1%
echo %x%
pause

cmd, vbs, ps, bash
autoit, python, swift

Offline

#4 27 Aug 2006 23:31

//[T.0.P]//
Member
Registered: 12 Aug 2006
Posts: 50

Re: FOR /F Long File Names in batch files

Sounds like you got a pretty heavy duty script going there. Is in being used now at the cafe? Well I'm glad I could help. Feel free to post again if you need any help

Offline

Board footer

Powered by FluxBB