Jump to content

for in CMD scripts


Recommended Posts

Hello,

I try to be as sort as possible.

I try to do some code in batch and cmd.exe (running Windows). Nwo I try the basic: I try to echo subdirectories listed in a folder.


for /r %%x in (.\www.slv.dk\Dokumenter\dsweb\Get) do echo "%%x\"

This should list the directories in Get folder. I know there is a bug, because I got this list of files as output:


C:/program files/GnuWin32/bin/./www.slv.dk/Dokumenter/dsweb/Get/
C:/program files/GnuWin32/bin/www.slv.dk/./www.slv.dk/Dokumenter/dsweb/Get/
C:/program files/GnuWin32/bin/www.slv.dk/Dokumenter/./www.slv.dk/Dokumenter/dsweb/Get/

Once again:

C:/program files/GnuWin32/bin/./www.slv.dk/Dokumenter/dsweb/Get/

C:/program files/GnuWin32/bin/www.slv.dk/./www.slv.dk/Dokumenter/dsweb/Get/

C:/program files/GnuWin32/bin/www.slv.dk/Dokumenter/./www.slv.dk/Dokumenter/dsweb/Get/

The marked text is incorrect / non-existing path.

It means there is added "./www.slv.dk/Dokumenter/dsweb/Get/" on end of %%x variable.

But I don't understand why there is listed:

C:/program files/GnuWin32/bin/www.slv.dk/

and

C:/program files/GnuWin32/bin/www.slv.dk/Dokumenter/

instead of the /Get directory???

I would expect something like this:

C:/program files/GnuWin32/bin/./www.slv.dk/Dokumenter/dsweb/Get/Document-900

C:/program files/GnuWin32/bin/./www.slv.dk/Dokumenter/dsweb/Get/Document-901

C:/program files/GnuWin32/bin/./www.slv.dk/Dokumenter/dsweb/Get/Document-902

etc

Is there any simple solution how to make the for echo correct folders? The echo command is just 1st step; I would change it later to this command


rename %%x\BG_AD_3_BG*.pdf _*.pdf

Link to comment
Share on other sites


Oops. Thanx for help. It works. I got the command that I needed to rename my files.

@Echo off
cls
echo on
for /r ".\www.slv.dk\Dokumenter\dsweb\Get" %%x in (.) do rename %%x\BG_AD_3_BG*.pdf _*.pdf & pause
pause

I originally wanted to rename *.pdf files and remove the BG_AD_3_BG string from begin of the name.

Link to comment
Share on other sites

I'd suggest something more along these lines:

@ECHO OFF & SETLOCAL ENABLEEXTENSIONS
REM Path to parent directory for recursion
(SET _P=WWW.SLV.DK\DOKUMENTER\DSWEB\GET)
REM Old filename string to replace
(SET _O=BG_AD_3_BG)
REM New filename string to add
(SET _N=_)
IF NOT EXIST "%_P%" GOTO :EOF
IF /I NOT "%CD%"=="%_P%" (PUSHD %_P% && SET "_=T")
FOR /D /R %%# IN (*) DO REN "%%#\%_O%*.PDF" "%_N%*.pdf"
IF %_%==T POPD
PAUSE

Link to comment
Share on other sites

It doesn't work for me. It looks that the last part of command

rename _K_ENR_*.PDF" "_*.pdf"

doesn't work.

Four last lines from the screen output:


c:\program files\GnuWin32\bin\www.slv.dk\Dokumenter\dsweb\Get>REN "c:\program files\GnuWin32\bin\www.slv.dk\Dokumenter\dsweb\Get\Document-6464\_K_ENR_*.PDF" "_*.pdf"

c:\program files\GnuWin32\bin\www.slv.dk\Dokumenter\dsweb\Get>REN "c:\program files\GnuWin32\bin\www.slv.dk\Dokumenter\dsweb\Get\Document-8808\_K_ENR_*.PDF" "_*.pdf"

c:\program files\GnuWin32\bin\www.slv.dk\Dokumenter\dsweb\Get>IF T == T POPD

c:\program files\GnuWin32\bin>PAUSE
Press any key to continue...

Edited by DosCode
Link to comment
Share on other sites

'The command before' is simply bad.

You don't have to remember it, it's written down! All you need to do is change the three lines according to your specification, (lines 3, 5 and 7), which I completed for you for your particular situation.

I'm not going to explain the basic questions you've asked, (you can learn the majority of these within the console window itself). Suffice it to say if you are asking these questions, you should certainly not be in a position to suggest that your effort is in some way better than mine.

<Edit />

I didn't change the functionality of what you asked, I simply fixed the poor example you gave. You provided the RENAME command you wanted to use, if that was incorrect too then you cannot pass the blame onto me. The output you have provided is EXACTLY what is intended according to your request.

If you wish something different, I'd suggest you provide us with your actual file/directory structures and names and an actual example of what you are wishing to change. That way we can create something specific to your situation.

Link to comment
Share on other sites

List of files


C:\program files\GnuWin32\bin\wget.exe
C:\program files\GnuWin32\bin\www.slv.dk
C:\program files\GnuWin32\bin\www.slv.dk\Dokumenter
C:\program files\GnuWin32\bin\www.slv.dk\Dokumenter\dsweb
C:\program files\GnuWin32\bin\www.slv.dk\Dokumenter\dsweb\Get
C:\program files\GnuWin32\bin\www.slv.dk\Dokumenter\dsweb\Get\Document-10698
C:\program files\GnuWin32\bin\www.slv.dk\Dokumenter\dsweb\Get\Document-11344
C:\program files\GnuWin32\bin\www.slv.dk\Dokumenter\dsweb\Get\Document-11345
C:\program files\GnuWin32\bin\www.slv.dk\Dokumenter\dsweb\Get\Document-11346
C:\program files\GnuWin32\bin\www.slv.dk\Dokumenter\dsweb\Get\Document-1402
C:\program files\GnuWin32\bin\www.slv.dk\Dokumenter\dsweb\Get\Document-1403
C:\program files\GnuWin32\bin\www.slv.dk\Dokumenter\dsweb\Get\Document-1404
C:\program files\GnuWin32\bin\www.slv.dk\Dokumenter\dsweb\Get\Document-1405
C:\program files\GnuWin32\bin\www.slv.dk\Dokumenter\dsweb\Get\Document-1406
C:\program files\GnuWin32\bin\www.slv.dk\Dokumenter\dsweb\Get\Document-1407
C:\program files\GnuWin32\bin\www.slv.dk\Dokumenter\dsweb\Get\Document-1408
C:\program files\GnuWin32\bin\www.slv.dk\Dokumenter\dsweb\Get\Document-1409
C:\program files\GnuWin32\bin\www.slv.dk\Dokumenter\dsweb\Get\Document-1410
C:\program files\GnuWin32\bin\www.slv.dk\Dokumenter\dsweb\Get\Document-1411
C:\program files\GnuWin32\bin\www.slv.dk\Dokumenter\dsweb\Get\Document-1412
C:\program files\GnuWin32\bin\www.slv.dk\Dokumenter\dsweb\Get\Document-1413
C:\program files\GnuWin32\bin\www.slv.dk\Dokumenter\dsweb\Get\Document-1414
C:\program files\GnuWin32\bin\www.slv.dk\Dokumenter\dsweb\Get\Document-1415
C:\program files\GnuWin32\bin\www.slv.dk\Dokumenter\dsweb\Get\Document-1416
C:\program files\GnuWin32\bin\www.slv.dk\Dokumenter\dsweb\Get\Document-1417
C:\program files\GnuWin32\bin\www.slv.dk\Dokumenter\dsweb\Get\Document-1421
C:\program files\GnuWin32\bin\www.slv.dk\Dokumenter\dsweb\Get\Document-1422
C:\program files\GnuWin32\bin\www.slv.dk\Dokumenter\dsweb\Get\Document-1423
C:\program files\GnuWin32\bin\www.slv.dk\Dokumenter\dsweb\Get\Document-1424
C:\program files\GnuWin32\bin\www.slv.dk\Dokumenter\dsweb\Get\Document-1426
C:\program files\GnuWin32\bin\www.slv.dk\Dokumenter\dsweb\Get\Document-1427
C:\program files\GnuWin32\bin\www.slv.dk\Dokumenter\dsweb\Get\Document-1428
C:\program files\GnuWin32\bin\www.slv.dk\Dokumenter\dsweb\Get\Document-1429
C:\program files\GnuWin32\bin\www.slv.dk\Dokumenter\dsweb\Get\Document-1430
C:\program files\GnuWin32\bin\www.slv.dk\Dokumenter\dsweb\Get\Document-1431
C:\program files\GnuWin32\bin\www.slv.dk\Dokumenter\dsweb\Get\Document-1432
C:\program files\GnuWin32\bin\www.slv.dk\Dokumenter\dsweb\Get\Document-1433
C:\program files\GnuWin32\bin\www.slv.dk\Dokumenter\dsweb\Get\Document-1434
C:\program files\GnuWin32\bin\www.slv.dk\Dokumenter\dsweb\Get\Document-1435
C:\program files\GnuWin32\bin\www.slv.dk\Dokumenter\dsweb\Get\Document-1436
C:\program files\GnuWin32\bin\www.slv.dk\Dokumenter\dsweb\Get\Document-1437
C:\program files\GnuWin32\bin\www.slv.dk\Dokumenter\dsweb\Get\Document-6463
C:\program files\GnuWin32\bin\www.slv.dk\Dokumenter\dsweb\Get\Document-6464
C:\program files\GnuWin32\bin\www.slv.dk\Dokumenter\dsweb\Get\Document-8808
C:\program files\GnuWin32\bin\www.slv.dk\Dokumenter\dsweb\Get\Document-10698\_K_ENR_3_6_en.pdf
C:\program files\GnuWin32\bin\www.slv.dk\Dokumenter\dsweb\Get\Document-11344\_K_ENR_6_LAS_en.pdf
C:\program files\GnuWin32\bin\www.slv.dk\Dokumenter\dsweb\Get\Document-11345\_K_ENR_6_UAS_en.pdf
C:\program files\GnuWin32\bin\www.slv.dk\Dokumenter\dsweb\Get\Document-11346\_K_ENR_6_HMR_en.pdf
C:\program files\GnuWin32\bin\www.slv.dk\Dokumenter\dsweb\Get\Document-1402\_K_ENR_0_6_en.pdf
C:\program files\GnuWin32\bin\www.slv.dk\Dokumenter\dsweb\Get\Document-1403\_K_ENR_1_1_en.pdf
C:\program files\GnuWin32\bin\www.slv.dk\Dokumenter\dsweb\Get\Document-1404\ENR_1_10_amdt_7_2004_8JUL.pdf
C:\program files\GnuWin32\bin\www.slv.dk\Dokumenter\dsweb\Get\Document-1405\_K_ENR_1_11_en.pdf
C:\program files\GnuWin32\bin\www.slv.dk\Dokumenter\dsweb\Get\Document-1406\enr_1_12.pdf
C:\program files\GnuWin32\bin\www.slv.dk\Dokumenter\dsweb\Get\Document-1407\_K_ENR_1_13_en.pdf
C:\program files\GnuWin32\bin\www.slv.dk\Dokumenter\dsweb\Get\Document-1408\_K_ENR_1_14_en.pdf
C:\program files\GnuWin32\bin\www.slv.dk\Dokumenter\dsweb\Get\Document-1409\_K_ENR_1_2_en.pdf
C:\program files\GnuWin32\bin\www.slv.dk\Dokumenter\dsweb\Get\Document-1410\enr_1_3.pdf
C:\program files\GnuWin32\bin\www.slv.dk\Dokumenter\dsweb\Get\Document-1411\enr_1_4.pdf
C:\program files\GnuWin32\bin\www.slv.dk\Dokumenter\dsweb\Get\Document-1412\ENR_1_5_AMDT_2_2004_19FEB.pdf
C:\program files\GnuWin32\bin\www.slv.dk\Dokumenter\dsweb\Get\Document-1413\_K_ENR_1_6_en.pdf
C:\program files\GnuWin32\bin\www.slv.dk\Dokumenter\dsweb\Get\Document-1414\enr_1_7.pdf
C:\program files\GnuWin32\bin\www.slv.dk\Dokumenter\dsweb\Get\Document-1415\_K_ENR_1_8_en_02_07_AIRAC.pdf
C:\program files\GnuWin32\bin\www.slv.dk\Dokumenter\dsweb\Get\Document-1416\_K_ENR_1_9_en.pdf
C:\program files\GnuWin32\bin\www.slv.dk\Dokumenter\dsweb\Get\Document-1417\_K_ENR_2_1_en.pdf
C:\program files\GnuWin32\bin\www.slv.dk\Dokumenter\dsweb\Get\Document-1421\_K_ENR_3_3_en.pdf
C:\program files\GnuWin32\bin\www.slv.dk\Dokumenter\dsweb\Get\Document-1422\_K_ENR_3_4_en.pdf
C:\program files\GnuWin32\bin\www.slv.dk\Dokumenter\dsweb\Get\Document-1423\_K_ENR_3_5_en.pdf
C:\program files\GnuWin32\bin\www.slv.dk\Dokumenter\dsweb\Get\Document-1424\_K_ENR_4_1_en.pdf
C:\program files\GnuWin32\bin\www.slv.dk\Dokumenter\dsweb\Get\Document-1426\enr_4_2.pdf
C:\program files\GnuWin32\bin\www.slv.dk\Dokumenter\dsweb\Get\Document-1427\_K_ENR_4_3_en.pdf
C:\program files\GnuWin32\bin\www.slv.dk\Dokumenter\dsweb\Get\Document-1428\_K_ENR_4_4_en_10_07.pdf
C:\program files\GnuWin32\bin\www.slv.dk\Dokumenter\dsweb\Get\Document-1429\_K_ENR_5_1_en.pdf
C:\program files\GnuWin32\bin\www.slv.dk\Dokumenter\dsweb\Get\Document-1430\_K_ENR_5_2_en_03_07_pdf.pdf
C:\program files\GnuWin32\bin\www.slv.dk\Dokumenter\dsweb\Get\Document-1431\_K_ENR_5_3_en.pdf
C:\program files\GnuWin32\bin\www.slv.dk\Dokumenter\dsweb\Get\Document-1432\_K_ENR_5_4_en.pdf
C:\program files\GnuWin32\bin\www.slv.dk\Dokumenter\dsweb\Get\Document-1433\_K_ENR_5_5_en.pdf
C:\program files\GnuWin32\bin\www.slv.dk\Dokumenter\dsweb\Get\Document-1434\enr_5_6.pdf
C:\program files\GnuWin32\bin\www.slv.dk\Dokumenter\dsweb\Get\Document-1435\_K_ENR_6_1_en.pdf
C:\program files\GnuWin32\bin\www.slv.dk\Dokumenter\dsweb\Get\Document-1436\enr62-1.pdf
C:\program files\GnuWin32\bin\www.slv.dk\Dokumenter\dsweb\Get\Document-1437\enr62-3.pdf
C:\program files\GnuWin32\bin\www.slv.dk\Dokumenter\dsweb\Get\Document-6463\_K_ENR_3_1_en.pdf
C:\program files\GnuWin32\bin\www.slv.dk\Dokumenter\dsweb\Get\Document-6464\_K_ENR_3_2_en.pdf
C:\program files\GnuWin32\bin\www.slv.dk\Dokumenter\dsweb\Get\Document-8808\_K_ENR_2_2_en.pdf

There are files like _K_ENR_1_6_en.pdf where I want to remove the _K_ENR_ sting from name of file.

Thanks.

Link to comment
Share on other sites

There are files like _K_ENR_1_6_en.pdf where I want to remove the _K_ENR_ sting from name of file.

Do you want to remove it or change it to something else (and if this is the case) is the something else "fixed" or variable?

I.e. C:\program files\GnuWin32\bin\www.slv.dk\Dokumenter\dsweb\Get\Document-1413\_K_ENR_1_6_en.pdf should become:

  1. 1_6_en.pdf
  2. <somethingelse>1_6_en.pdf

Example (remove _K_ENR_ only )

@ECHO OFF
SETLOCAL ENABLE EXTENSIONS
FOR /F "tokens=* delims=" %%A IN ('DIR /B /S *.pdf') DO CALL :rename "%%A"
GOTO :EOF
:rename
SET filename=%~n1
SET Newfilename=%filename:_K_ENR_=%
ECHO %1
ECHO "%~dp1%Newfilename%%~x1"
GOTO :EOF

jaclaz

Edited by jaclaz
Link to comment
Share on other sites

@ECHO OFF
SETLOCAL ENABLE EXTENSIONS
FOR /F "tokens=* delims=" %%A IN ('DIR /B /S *.pdf') DO CALL :rename "%%A"
GOTO :EOF
:rename
SET filename=%~n1
SET Newfilename=%filename:_K_ENR_=%
ECHO %1
ECHO "%~dp1%Newfilename%%~x1"
GOTO :EOF

I think I'd be a little more specific with the for/call output:

@ECHO OFF
SETLOCAL ENABLEEXTENSIONS
FOR /F "TOKENS=*" %%# IN ('DIR/B/S/A-D "_K_ENR_*.pdf"') DO CALL :RN "%%#" "%%~nx#"
PAUSE
GOTO :EOF
:RN
SET "_FN=%~2"
ECHO=REN %1 "%_FN:~7%"

Link to comment
Share on other sites

Thanks.

I think I'd be a little more specific with the for/call output:

@ECHO OFF
SETLOCAL ENABLEEXTENSIONS
FOR /F "TOKENS=*" %%# IN ('DIR/B/S/A-D "_K_ENR_*.pdf"') DO CALL :RN "%%#" "%%~nx#"
PAUSE
GOTO :EOF
:RN
SET "_FN=%~2"
ECHO=REN %1 "%_FN:~7%"

The last line was not working for me. Nothing happened. But when I removed the "ECHO=" files are renamed correctly.

Link to comment
Share on other sites

The last line was not working for me. Nothing happened. But when I removed the "ECHO=" files are renamed correctly.

That was the point—so you could see and verify that the commands would be correct when run before committing to them.

Edited by 5eraph
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...