Page 1 of 1

for in DOS batch - MSFN Forum

Posted: 15 Feb 2012 14:20
by doscode
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.

Code: Select all

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:

Code: Select all

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

Code: Select all

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

Re: for in DOS batch - MSFN Forum

Posted: 15 Feb 2012 16:05
by Liviu
doscode wrote:

Code: Select all

for /r %%x in (.\www.slv.dk\Dokumenter\dsweb\Get) do echo "%%x\"
This should list the directories in Get folder.
No, that will actually run the 'for' loop without '/r' in the current directory and each of its subdirectories.

You probably want this, instead.

Code: Select all

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

Liviu

Re: for in DOS batch - MSFN Forum

Posted: 16 Feb 2012 05:24
by doscode
Thank you. Thats great!

Re: for in DOS batch - MSFN Forum

Posted: 16 Feb 2012 06:01
by doscode
Yet I wanted to rename the files, and tried it this way:

Code: Select all

for /r ".\www.slv.dk\Dokumenter\dsweb\Get" %%x in (.) do rename "%%~x\_K_ENR_*.pdf" _*.pdf
::for /r ".\www.slv.dk\Dokumenter\dsweb\Get" %%x in (.) do rename %%x\EK_ENR_*.pdf _*.pdf


But none of this works. When I run the commented command (line 2), to replace EK_ENR_*.pdf _*.pdf , I got instead files like _K_ENR_*.pdf ... So then I try to run the uncommented version (line 1) but nothing changes... Just to remove the EK_ENR_ string.

Re: for in DOS batch - MSFN Forum

Posted: 16 Feb 2012 06:21
by alan_b
Oops - misread the problem

Alan

Re: for in DOS batch - MSFN Forum

Posted: 16 Feb 2012 06:48
by foxidrive
This should work. Remove the 'echo' if it looks ok.

Code: Select all

@echo off
pushd "C:/program files/GnuWin32/bin"
for /f "delims=" %%a in ('dir BG_AD_3_BG*.pdf /b /s') do echo ren "%%a" "_%%~nxa"
popd



The FOR /D form of the for command has bugs and the FOR /R might too.

To date the FOR /F command performs well and you just add a DIR to perform the recursion or to just list files etc.

Re: for in DOS batch - MSFN Forum

Posted: 16 Feb 2012 06:54
by doscode
I originally thought that this command is working correctly,

Code: Select all

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

but not. Probably, when I tested it, I missed something.

Edit - foxidrive:
Just little change to actual situation in my directory:

Code: Select all

pushd "C:/program files/GnuWin32/bin"
for /f "delims=" %%a in ('dir _K_ENR_*.pdf /b /s') do echo ren "%%a" "_%%~nxa"
popd


This is the output:

Code: Select all

ren "C:\program files\GnuWin32\bin\www.slv.dk\Dokumenter\dsweb\Get\Document-
10698\_K_ENR_3_6_en.pdf" "__K_ENR_3_6_en.pdf"
ren "C:\program files\GnuWin32\bin\www.slv.dk\Dokumenter\dsweb\Get\Document-
11344\_K_ENR_6_LAS_en.pdf" "__K_ENR_6_LAS_en.pdf"
ren "C:\program files\GnuWin32\bin\www.slv.dk\Dokumenter\dsweb\Get\Document-
11345\_K_ENR_6_UAS_en.pdf" "__K_ENR_6_UAS_en.pdf"
ren "C:\program files\GnuWin32\bin\www.slv.dk\Dokumenter\dsweb\Get\Document-
11346\_K_ENR_6_HMR_en.pdf" "__K_ENR_6_HMR_en.pdf"
ren "C:\program files\GnuWin32\bin\www.slv.dk\Dokumenter\dsweb\Get\Document-
1402\_K_ENR_0_6_en.pdf" "__K_ENR_0_6_en.pdf"
ren "C:\program files\GnuWin32\bin\www.slv.dk\Dokumenter\dsweb\Get\Document-
1403\_K_ENR_1_1_en.pdf" "__K_ENR_1_1_en.pdf"
ren "C:\program files\GnuWin32\bin\www.slv.dk\Dokumenter\dsweb\Get\Document-
1405\_K_ENR_1_11_en.pdf" "__K_ENR_1_11_en.pdf"
ren "C:\program files\GnuWin32\bin\www.slv.dk\Dokumenter\dsweb\Get\Document-
1407\_K_ENR_1_13_en.pdf" "__K_ENR_1_13_en.pdf"
ren "C:\program files\GnuWin32\bin\www.slv.dk\Dokumenter\dsweb\Get\Document-
1408\_K_ENR_1_14_en.pdf" "__K_ENR_1_14_en.pdf"
ren "C:\program files\GnuWin32\bin\www.slv.dk\Dokumenter\dsweb\Get\Document-
1409\_K_ENR_1_2_en.pdf" "__K_ENR_1_2_en.pdf"
ren "C:\program files\GnuWin32\bin\www.slv.dk\Dokumenter\dsweb\Get\Document-
1413\_K_ENR_1_6_en.pdf" "__K_ENR_1_6_en.pdf"
ren "C:\program files\GnuWin32\bin\www.slv.dk\Dokumenter\dsweb\Get\Document-
1415\_K_ENR_1_8_en_02_07_AIRAC.pdf" "__K_ENR_1_8_en_02_07_AIRAC.pdf"
ren "C:\program files\GnuWin32\bin\www.slv.dk\Dokumenter\dsweb\Get\Document-
1416\_K_ENR_1_9_en.pdf" "__K_ENR_1_9_en.pdf"
ren "C:\program files\GnuWin32\bin\www.slv.dk\Dokumenter\dsweb\Get\Document-
1417\_K_ENR_2_1_en.pdf" "__K_ENR_2_1_en.pdf"
ren "C:\program files\GnuWin32\bin\www.slv.dk\Dokumenter\dsweb\Get\Document-
1421\_K_ENR_3_3_en.pdf" "__K_ENR_3_3_en.pdf"
ren "C:\program files\GnuWin32\bin\www.slv.dk\Dokumenter\dsweb\Get\Document-
1422\_K_ENR_3_4_en.pdf" "__K_ENR_3_4_en.pdf"
ren "C:\program files\GnuWin32\bin\www.slv.dk\Dokumenter\dsweb\Get\Document-
1423\_K_ENR_3_5_en.pdf" "__K_ENR_3_5_en.pdf"
ren "C:\program files\GnuWin32\bin\www.slv.dk\Dokumenter\dsweb\Get\Document-
1424\_K_ENR_4_1_en.pdf" "__K_ENR_4_1_en.pdf"
ren "C:\program files\GnuWin32\bin\www.slv.dk\Dokumenter\dsweb\Get\Document-
1427\_K_ENR_4_3_en.pdf" "__K_ENR_4_3_en.pdf"
ren "C:\program files\GnuWin32\bin\www.slv.dk\Dokumenter\dsweb\Get\Document-
1428\_K_ENR_4_4_en_10_07.pdf" "__K_ENR_4_4_en_10_07.pdf"
ren "C:\program files\GnuWin32\bin\www.slv.dk\Dokumenter\dsweb\Get\Document-
1429\_K_ENR_5_1_en.pdf" "__K_ENR_5_1_en.pdf"
ren "C:\program files\GnuWin32\bin\www.slv.dk\Dokumenter\dsweb\Get\Document-
1430\_K_ENR_5_2_en_03_07_pdf.pdf" "__K_ENR_5_2_en_03_07_pdf.pdf"
ren "C:\program files\GnuWin32\bin\www.slv.dk\Dokumenter\dsweb\Get\Document-
1431\_K_ENR_5_3_en.pdf" "__K_ENR_5_3_en.pdf"
ren "C:\program files\GnuWin32\bin\www.slv.dk\Dokumenter\dsweb\Get\Document-
1432\_K_ENR_5_4_en.pdf" "__K_ENR_5_4_en.pdf"
ren "C:\program files\GnuWin32\bin\www.slv.dk\Dokumenter\dsweb\Get\Document-
1433\_K_ENR_5_5_en.pdf" "__K_ENR_5_5_en.pdf"
ren "C:\program files\GnuWin32\bin\www.slv.dk\Dokumenter\dsweb\Get\Document-
1435\_K_ENR_6_1_en.pdf" "__K_ENR_6_1_en.pdf"
ren "C:\program files\GnuWin32\bin\www.slv.dk\Dokumenter\dsweb\Get\Document-
6463\_K_ENR_3_1_en.pdf" "__K_ENR_3_1_en.pdf"
ren "C:\program files\GnuWin32\bin\www.slv.dk\Dokumenter\dsweb\Get\Document-
6464\_K_ENR_3_2_en.pdf" "__K_ENR_3_2_en.pdf"
ren "C:\program files\GnuWin32\bin\www.slv.dk\Dokumenter\dsweb\Get\Document-
8808\_K_ENR_2_2_en.pdf" "__K_ENR_2_2_en.pdf"
Press any key to continue


No change at all.

Re: for in DOS batch - MSFN Forum

Posted: 16 Feb 2012 07:09
by Squashman
doscode wrote: I would change it later to this command

Code: Select all

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


You should have just stated this as your intended goal. The extra comments you made above are unrelated enough that the code changes and then the script needs to be re-written.

Re: for in DOS batch - MSFN Forum

Posted: 16 Feb 2012 07:35
by foxidrive

Code: Select all

pushd "C:/program files/GnuWin32/bin"
for /f "delims=" %%a in ('dir _K_ENR_*.pdf /b /s') do echo ren "%%a" "_%%~nxa"
popd


This is the output:

Code: Select all

ren "C:\program files\GnuWin32\bin\www.slv.dk\Dokumenter\dsweb\Get\Document-10698\_K_ENR_3_6_en.pdf" "__K_ENR_3_6_en.pdf"


No change at all.


What do you expect it to do? I'm a little lost.

The ren command adds the extra underscore to the start of the filename, if you remove the echo so that it is actually renamed.

Re: for in DOS batch - MSFN Forum

Posted: 16 Feb 2012 08:04
by doscode
Well, the first part where I tried to echo was OK and working. But then I tried to rename files beginning by some string for example _K_ENR_ and to remove this string from the name of file so the filename was not changed at all or incorrectly.

Re: for in DOS batch - MSFN Forum

Posted: 16 Feb 2012 08:45
by foxidrive
Sorry mate, your messages are confusing and lack detail.

If something doesn't work then please describe how it doesn't work, how it fails, and what command or code you used, and what you actually want to do.

Your example added a _ to the start of the name and that is what the code I provided does when used properly and assuming there are not already files with those names.