DosCode Posted February 15, 2012 Share Posted February 15, 2012 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/andC:/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-900C:/program files/GnuWin32/bin/./www.slv.dk/Dokumenter/dsweb/Get/Document-901C:/program files/GnuWin32/bin/./www.slv.dk/Dokumenter/dsweb/Get/Document-902etcIs 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 commandrename %%x\BG_AD_3_BG*.pdf _*.pdf Link to comment Share on other sites More sharing options...
allen2 Posted February 15, 2012 Share Posted February 15, 2012 For /R doesn't do what you think it does: Read the help there. Link to comment Share on other sites More sharing options...
DosCode Posted February 15, 2012 Author Share Posted February 15, 2012 Oops. Thanx for help. It works. I got the command that I needed to rename my files.@Echo offclsecho onfor /r ".\www.slv.dk\Dokumenter\dsweb\Get" %%x in (.) do rename %%x\BG_AD_3_BG*.pdf _*.pdf & pausepauseI 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 More sharing options...
Yzöwl Posted February 15, 2012 Share Posted February 15, 2012 I'd suggest something more along these lines:@ECHO OFF & SETLOCAL ENABLEEXTENSIONSREM 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 :EOFIF /I NOT "%CD%"=="%_P%" (PUSHD %_P% && SET "_=T")FOR /D /R %%# IN (*) DO REN "%%#\%_O%*.PDF" "%_N%*.pdf"IF %_%==T POPDPAUSE Link to comment Share on other sites More sharing options...
DosCode Posted February 16, 2012 Author Share Posted February 16, 2012 But this is more hard to remmeber. The command before is very simple.Can I ask you what does mean"%_P%""%CD%"PUSHD SET "_=T"FOR /DT POPD? Link to comment Share on other sites More sharing options...
DosCode Posted February 16, 2012 Author Share Posted February 16, 2012 (edited) 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 POPDc:\program files\GnuWin32\bin>PAUSEPress any key to continue... Edited February 16, 2012 by DosCode Link to comment Share on other sites More sharing options...
Yzöwl Posted February 16, 2012 Share Posted February 16, 2012 '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 More sharing options...
DosCode Posted February 16, 2012 Author Share Posted February 16, 2012 List of filesC:\program files\GnuWin32\bin\wget.exeC:\program files\GnuWin32\bin\www.slv.dkC:\program files\GnuWin32\bin\www.slv.dk\DokumenterC:\program files\GnuWin32\bin\www.slv.dk\Dokumenter\dswebC:\program files\GnuWin32\bin\www.slv.dk\Dokumenter\dsweb\GetC:\program files\GnuWin32\bin\www.slv.dk\Dokumenter\dsweb\Get\Document-10698C:\program files\GnuWin32\bin\www.slv.dk\Dokumenter\dsweb\Get\Document-11344C:\program files\GnuWin32\bin\www.slv.dk\Dokumenter\dsweb\Get\Document-11345C:\program files\GnuWin32\bin\www.slv.dk\Dokumenter\dsweb\Get\Document-11346C:\program files\GnuWin32\bin\www.slv.dk\Dokumenter\dsweb\Get\Document-1402C:\program files\GnuWin32\bin\www.slv.dk\Dokumenter\dsweb\Get\Document-1403C:\program files\GnuWin32\bin\www.slv.dk\Dokumenter\dsweb\Get\Document-1404C:\program files\GnuWin32\bin\www.slv.dk\Dokumenter\dsweb\Get\Document-1405C:\program files\GnuWin32\bin\www.slv.dk\Dokumenter\dsweb\Get\Document-1406C:\program files\GnuWin32\bin\www.slv.dk\Dokumenter\dsweb\Get\Document-1407C:\program files\GnuWin32\bin\www.slv.dk\Dokumenter\dsweb\Get\Document-1408C:\program files\GnuWin32\bin\www.slv.dk\Dokumenter\dsweb\Get\Document-1409C:\program files\GnuWin32\bin\www.slv.dk\Dokumenter\dsweb\Get\Document-1410C:\program files\GnuWin32\bin\www.slv.dk\Dokumenter\dsweb\Get\Document-1411C:\program files\GnuWin32\bin\www.slv.dk\Dokumenter\dsweb\Get\Document-1412C:\program files\GnuWin32\bin\www.slv.dk\Dokumenter\dsweb\Get\Document-1413C:\program files\GnuWin32\bin\www.slv.dk\Dokumenter\dsweb\Get\Document-1414C:\program files\GnuWin32\bin\www.slv.dk\Dokumenter\dsweb\Get\Document-1415C:\program files\GnuWin32\bin\www.slv.dk\Dokumenter\dsweb\Get\Document-1416C:\program files\GnuWin32\bin\www.slv.dk\Dokumenter\dsweb\Get\Document-1417C:\program files\GnuWin32\bin\www.slv.dk\Dokumenter\dsweb\Get\Document-1421C:\program files\GnuWin32\bin\www.slv.dk\Dokumenter\dsweb\Get\Document-1422C:\program files\GnuWin32\bin\www.slv.dk\Dokumenter\dsweb\Get\Document-1423C:\program files\GnuWin32\bin\www.slv.dk\Dokumenter\dsweb\Get\Document-1424C:\program files\GnuWin32\bin\www.slv.dk\Dokumenter\dsweb\Get\Document-1426C:\program files\GnuWin32\bin\www.slv.dk\Dokumenter\dsweb\Get\Document-1427C:\program files\GnuWin32\bin\www.slv.dk\Dokumenter\dsweb\Get\Document-1428C:\program files\GnuWin32\bin\www.slv.dk\Dokumenter\dsweb\Get\Document-1429C:\program files\GnuWin32\bin\www.slv.dk\Dokumenter\dsweb\Get\Document-1430C:\program files\GnuWin32\bin\www.slv.dk\Dokumenter\dsweb\Get\Document-1431C:\program files\GnuWin32\bin\www.slv.dk\Dokumenter\dsweb\Get\Document-1432C:\program files\GnuWin32\bin\www.slv.dk\Dokumenter\dsweb\Get\Document-1433C:\program files\GnuWin32\bin\www.slv.dk\Dokumenter\dsweb\Get\Document-1434C:\program files\GnuWin32\bin\www.slv.dk\Dokumenter\dsweb\Get\Document-1435C:\program files\GnuWin32\bin\www.slv.dk\Dokumenter\dsweb\Get\Document-1436C:\program files\GnuWin32\bin\www.slv.dk\Dokumenter\dsweb\Get\Document-1437C:\program files\GnuWin32\bin\www.slv.dk\Dokumenter\dsweb\Get\Document-6463C:\program files\GnuWin32\bin\www.slv.dk\Dokumenter\dsweb\Get\Document-6464C:\program files\GnuWin32\bin\www.slv.dk\Dokumenter\dsweb\Get\Document-8808C:\program files\GnuWin32\bin\www.slv.dk\Dokumenter\dsweb\Get\Document-10698\_K_ENR_3_6_en.pdfC:\program files\GnuWin32\bin\www.slv.dk\Dokumenter\dsweb\Get\Document-11344\_K_ENR_6_LAS_en.pdfC:\program files\GnuWin32\bin\www.slv.dk\Dokumenter\dsweb\Get\Document-11345\_K_ENR_6_UAS_en.pdfC:\program files\GnuWin32\bin\www.slv.dk\Dokumenter\dsweb\Get\Document-11346\_K_ENR_6_HMR_en.pdfC:\program files\GnuWin32\bin\www.slv.dk\Dokumenter\dsweb\Get\Document-1402\_K_ENR_0_6_en.pdfC:\program files\GnuWin32\bin\www.slv.dk\Dokumenter\dsweb\Get\Document-1403\_K_ENR_1_1_en.pdfC:\program files\GnuWin32\bin\www.slv.dk\Dokumenter\dsweb\Get\Document-1404\ENR_1_10_amdt_7_2004_8JUL.pdfC:\program files\GnuWin32\bin\www.slv.dk\Dokumenter\dsweb\Get\Document-1405\_K_ENR_1_11_en.pdfC:\program files\GnuWin32\bin\www.slv.dk\Dokumenter\dsweb\Get\Document-1406\enr_1_12.pdfC:\program files\GnuWin32\bin\www.slv.dk\Dokumenter\dsweb\Get\Document-1407\_K_ENR_1_13_en.pdfC:\program files\GnuWin32\bin\www.slv.dk\Dokumenter\dsweb\Get\Document-1408\_K_ENR_1_14_en.pdfC:\program files\GnuWin32\bin\www.slv.dk\Dokumenter\dsweb\Get\Document-1409\_K_ENR_1_2_en.pdfC:\program files\GnuWin32\bin\www.slv.dk\Dokumenter\dsweb\Get\Document-1410\enr_1_3.pdfC:\program files\GnuWin32\bin\www.slv.dk\Dokumenter\dsweb\Get\Document-1411\enr_1_4.pdfC:\program files\GnuWin32\bin\www.slv.dk\Dokumenter\dsweb\Get\Document-1412\ENR_1_5_AMDT_2_2004_19FEB.pdfC:\program files\GnuWin32\bin\www.slv.dk\Dokumenter\dsweb\Get\Document-1413\_K_ENR_1_6_en.pdfC:\program files\GnuWin32\bin\www.slv.dk\Dokumenter\dsweb\Get\Document-1414\enr_1_7.pdfC:\program files\GnuWin32\bin\www.slv.dk\Dokumenter\dsweb\Get\Document-1415\_K_ENR_1_8_en_02_07_AIRAC.pdfC:\program files\GnuWin32\bin\www.slv.dk\Dokumenter\dsweb\Get\Document-1416\_K_ENR_1_9_en.pdfC:\program files\GnuWin32\bin\www.slv.dk\Dokumenter\dsweb\Get\Document-1417\_K_ENR_2_1_en.pdfC:\program files\GnuWin32\bin\www.slv.dk\Dokumenter\dsweb\Get\Document-1421\_K_ENR_3_3_en.pdfC:\program files\GnuWin32\bin\www.slv.dk\Dokumenter\dsweb\Get\Document-1422\_K_ENR_3_4_en.pdfC:\program files\GnuWin32\bin\www.slv.dk\Dokumenter\dsweb\Get\Document-1423\_K_ENR_3_5_en.pdfC:\program files\GnuWin32\bin\www.slv.dk\Dokumenter\dsweb\Get\Document-1424\_K_ENR_4_1_en.pdfC:\program files\GnuWin32\bin\www.slv.dk\Dokumenter\dsweb\Get\Document-1426\enr_4_2.pdfC:\program files\GnuWin32\bin\www.slv.dk\Dokumenter\dsweb\Get\Document-1427\_K_ENR_4_3_en.pdfC:\program files\GnuWin32\bin\www.slv.dk\Dokumenter\dsweb\Get\Document-1428\_K_ENR_4_4_en_10_07.pdfC:\program files\GnuWin32\bin\www.slv.dk\Dokumenter\dsweb\Get\Document-1429\_K_ENR_5_1_en.pdfC:\program files\GnuWin32\bin\www.slv.dk\Dokumenter\dsweb\Get\Document-1430\_K_ENR_5_2_en_03_07_pdf.pdfC:\program files\GnuWin32\bin\www.slv.dk\Dokumenter\dsweb\Get\Document-1431\_K_ENR_5_3_en.pdfC:\program files\GnuWin32\bin\www.slv.dk\Dokumenter\dsweb\Get\Document-1432\_K_ENR_5_4_en.pdfC:\program files\GnuWin32\bin\www.slv.dk\Dokumenter\dsweb\Get\Document-1433\_K_ENR_5_5_en.pdfC:\program files\GnuWin32\bin\www.slv.dk\Dokumenter\dsweb\Get\Document-1434\enr_5_6.pdfC:\program files\GnuWin32\bin\www.slv.dk\Dokumenter\dsweb\Get\Document-1435\_K_ENR_6_1_en.pdfC:\program files\GnuWin32\bin\www.slv.dk\Dokumenter\dsweb\Get\Document-1436\enr62-1.pdfC:\program files\GnuWin32\bin\www.slv.dk\Dokumenter\dsweb\Get\Document-1437\enr62-3.pdfC:\program files\GnuWin32\bin\www.slv.dk\Dokumenter\dsweb\Get\Document-6463\_K_ENR_3_1_en.pdfC:\program files\GnuWin32\bin\www.slv.dk\Dokumenter\dsweb\Get\Document-6464\_K_ENR_3_2_en.pdfC:\program files\GnuWin32\bin\www.slv.dk\Dokumenter\dsweb\Get\Document-8808\_K_ENR_2_2_en.pdfThere 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 More sharing options...
jaclaz Posted February 16, 2012 Share Posted February 16, 2012 (edited) 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_6_en.pdf<somethingelse>1_6_en.pdfExample (remove _K_ENR_ only )@ECHO OFFSETLOCAL ENABLE EXTENSIONSFOR /F "tokens=* delims=" %%A IN ('DIR /B /S *.pdf') DO CALL :rename "%%A"GOTO :EOF:renameSET filename=%~n1SET Newfilename=%filename:_K_ENR_=%ECHO %1ECHO "%~dp1%Newfilename%%~x1"GOTO :EOFjaclaz Edited February 16, 2012 by jaclaz Link to comment Share on other sites More sharing options...
DosCode Posted February 16, 2012 Author Share Posted February 16, 2012 Yes I want to remove it. I originally didn't find how to do it, so I tried to replace it for "_" but the best solution would be to remove it. Link to comment Share on other sites More sharing options...
jaclaz Posted February 16, 2012 Share Posted February 16, 2012 Yes I want to remove it. Then you have an example to work on. jaclaz Link to comment Share on other sites More sharing options...
Yzöwl Posted February 16, 2012 Share Posted February 16, 2012 @ECHO OFFSETLOCAL ENABLE EXTENSIONSFOR /F "tokens=* delims=" %%A IN ('DIR /B /S *.pdf') DO CALL :rename "%%A"GOTO :EOF:renameSET filename=%~n1SET Newfilename=%filename:_K_ENR_=%ECHO %1ECHO "%~dp1%Newfilename%%~x1"GOTO :EOFI think I'd be a little more specific with the for/call output:@ECHO OFFSETLOCAL ENABLEEXTENSIONSFOR /F "TOKENS=*" %%# IN ('DIR/B/S/A-D "_K_ENR_*.pdf"') DO CALL :RN "%%#" "%%~nx#"PAUSEGOTO :EOF:RNSET "_FN=%~2"ECHO=REN %1 "%_FN:~7%" Link to comment Share on other sites More sharing options...
jaclaz Posted February 16, 2012 Share Posted February 16, 2012 I think I'd be a little more specific with the for/call output:Sure .The idea of an example is that of an example. IMHO, not that of a "refined" solution.jaclaz Link to comment Share on other sites More sharing options...
DosCode Posted February 16, 2012 Author Share Posted February 16, 2012 Thanks.I think I'd be a little more specific with the for/call output:@ECHO OFFSETLOCAL ENABLEEXTENSIONSFOR /F "TOKENS=*" %%# IN ('DIR/B/S/A-D "_K_ENR_*.pdf"') DO CALL :RN "%%#" "%%~nx#"PAUSEGOTO :EOF:RNSET "_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 More sharing options...
Guest Posted February 16, 2012 Share Posted February 16, 2012 (edited) 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 February 16, 2012 by 5eraph Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now