I have a for loop in a batch file:
#echo off
set logpath1=C:\path\to\first\log
set logpath2=C:\path\to\second\log
FOR %%G IN (%logpath1% %logpath2%) DO (
pushd %%G
pushd ..\
for %%D IN (%CD%) DO SET "dirname=%%~nxD"
popd
echo Will prefix files in %%G with %supername%
:: do operations.
popd
)
However, this doesn't seem to change my working directory. If I run the script from
C:\tmp\path\to
then I get
Will prefix files in C:\path\to\first\log with tmp
Will prefix files in C:\path\to\second\log with tmp
My purpose is to zip up log files in multiple directories. I've tried chdir, cd /d, and &~pd0 instead of %CD%, and searched high and low. Can anyone tell my why this doesn't work??
Put
setlocal enabledelayedexpansion
at the top of your batch file and use !CD! instead of %CD% in the loop. Depending on where %supername% is set you may need to use ! there too.
Related
I know there's a ton of stuff out there on this but I just can't make it work. What I want to do is
#ECHO OFF
FOR /D %%D IN (*) Do (
ECHO %%D
if not exists %%D.mp4 (
rem cd %%D
rem "C:\Program Files\Handbrake\HandBrakeCLI.exe" -i "VIDEO_TS" -o "..\%%D.mp4" --preset="High Profile" --x264-preset slow
echo not exists
)
rem cd ..\
)
I left the extra stuff in there so you could see that I actually need the /D rather than a /R. basically I'm converting the VIDEO_TS folder that is in an other folder into an mp4 of the parents name and putting it in the root.
the problem is checking if the file already exists. I can't figure out how to create a path that "exists" will recognize out of the var %%D and the suffix .mp4
Any help would be greatly appreciated.
Thanks,
R
There are two errors here
The keyword is EXIST, not EXISTS.
You need to enclose the variable in double-quotes, due to spaces in filenames.
Example:
#ECHO OFF
FOR /D %%D IN (*) DO (
ECHO %%D
IF NOT EXIST "%%D.mp4" (
ECHO not exists
)
)
Further, if you uncomment those other lines, you'll have issues because the cd .. is not at the same branching level as the cd "%%D" (note I added double-quotes here). You should move that into the IF block.
However, instead of changing directory, you could simply pass "%%D\VIDEO_TS" to Handbrake.
FOR /D %%D IN (*) DO (
IF NOT EXIST "%%D.mp4" (
echo Encoding %%D ...
"C:\Program Files\Handbrake\HandBrakeCLI.exe" -i "%%D\VIDEO_TS" -o "%%D.mp4" --preset="High Profile" --x264-preset slow
)
)
Or you could use pushd and popd:
FOR /D %%D IN (*) DO (
pushd .
IF NOT EXIST "%%D.mp4" (
cd "%%D"
echo Encoding %%D ...
"C:\Program Files\Handbrake\HandBrakeCLI.exe" -i "VIDEO_TS" -o "..\%%D.mp4" --preset="High Profile" --x264-preset slow
)
popd
)
I have a batch file that checks a root directory every 10 seconds for PDF files and copies those files to their corresponding subfolders of the exact same name.
What I'm missing is that I need my batch file to check the target subdirectory for the named file, and if the named file exists, to rename the new file to be copied to !filename!001.pdf and have the 001 get incremented as duplicate file names are copied. Here's what I got:
:loop
setlocal enabledelayedexpansion
cls
pushd c:\files\
for /f "tokens=*" %%1 in ('dir /a-d /b c:\files\*.pdf') do (
set filename=%%~n1&set dirname=!filename:~0,7!
dir c:\files /b *.pdf > location1list.tmp
for /f %%a in (location1list.tmp) do dir c:\files\%%a > location2list.tmp
if not exist c:\files\!dirname! (md c:\files\!dirname!)
move %%1 c:\files\!dirname!\>nul
)
timeout /t 10
goto:loop
Any suggestions how I can get the files renamed?
I.e. if Bob.pdf exists in the Bob folder and another Bob.pdf is added to the C:\files folder, I want it to be copied to the C:\files\bob folder as Bob001.pdf.
Something like this should do what you want. May need some tweaking. Also I have not tested it, just wrote it from memory. Need any explanations, just ask.
Script
#echo off
setlocal EnableExtensions EnableDelayedExpansion
pushd C:\files
:Main
for /f "tokens=*" %%A in ('dir /a-d /b *.pdf') do (
if not exist "%%~dpnA\*" md "%%~nA"
set "File=%%~dpnA\%%~nxA"
if exist "!File!" call :Name "!File!" File
echo !File!
move "%%~fA" "!File!"
)
goto Wait
:Name <Target> <Variable>
set "Count=0"
:Count
set /a "Count+=1"
set "Number=00%Count%"
if exist "%~dpn1%Number:~-3%%~x1" goto Count
set "%~2=%~dpn1%Number:~-3%%~x1"
goto :eof
:Wait
timeout /t 10
goto Main
:End
popd
endlocal
Limitations
Each target subdirectory can only handle a maximum of 1000 files as this is written. If the subdirectory hits this maximum then the script will get stuck in an infinite loop.
Update
Fixed an error in my initial script
I'm wanting to change the icon of every sub folder in a particular directory, can I do this by means of a loop similar to the %%~nxf used for files (I’m after folders not files)
attrib -h -r C:\ICT\AutoCAD_2010\Customisations\AO2\EXE\desktop.ini
echo [.ShellClassInfo] >C:\ICT\AutoCAD_2010\Customisations\AO2\EXE\desktop.ini
echo IconFile= C:\ICT\AutoCAD_2010\Customisations\AO2\ICO\FolderIcon.ico >>C:\ICT\AutoCAD_2010\Customisations\AO2\EXE\desktop.ini
echo InfoTip=EXE file location... >>C:\ICT\AutoCAD_2010\Customisations\AO2\EXE\desktop.ini
echo IconIndex=0 >>C:\ICT\AutoCAD_2010\Customisations\AO2\EXE\desktop.ini
attrib +h +r C:\ICT\AutoCAD_2010\Customisations\AO2\EXE\desktop.ini
attrib +r C:\ICT\AutoCAD_2010\Customisations\AO2\EXE
Of cause do this a couple of hundred times in a batch and well you get the idea.
You may process folders (not files) with /D switch of for command:
for /D %%a in (*) do echo Folder: %%a
You may also combine /R with /D to process every subfolder beneath the given path. For further details, type: for /?
What im trying to do is convert .ogg files stored in many different folders. What i NEED the batch script to do is go from the first folder and file , convert it and only then move on to the next file in the folder. Also during the conversion process a wave file is created, so i need it to be deleted at the end of the files conversion along with the original .ogg file. This is my code so far. It goes through every folder and file performing the oggdec producing massive amounts of wave files. I need the script to only do one folder and one file at a time
for /r %%i in (*.ogg) do oggdec "%%i"
for /r %%i in (*.ogg) do del "%%i"
for /r %%i in (*.wav) do lame -m m "%%i"
for /r %%i in (*.wav) do del "%%i"
#echo off
etlocal enabledelayedexpansion
set deletestring=.wav
echo Ready to start
echo.
echo.
for /f "delims==" %%F in ('dir /b /s /a-d ^| find "%deletestring%"') do (
set oldfilename=%%~nxF
set pathname=%%~dpF
set newfilename=!oldfilename:%deletestring%=!
Ren "!pathname!!oldfilename!" "!newfilename!"
)
echo.
echo All done
(
This code above almost works in that the end result is working, but unfortunately i need it to convert only one file at a time and delete the original .ogg and .wav before moving to the next .ogg file.So i need it to follow this rule
Take the first folder and the first .ogg file do oggdec on file producing the .wav del the .ogg file do lame -m m on the .wav file producing the .mp3 file del the .wav file move on to the next .ogg file until all files in the folder are converted then move on to the next folder which may not be in the same folder.
An example folder structure.
Sounds/voice/greek
Sounds/voice/English
Sounds/voice/russion
Using enhanced substitution for FOR variable:
%%~di - drive letter
%%~pi - file path
%%~ni - file name without extension
Resulting script is:
for /r %%i in (*.ogg) do (
oggdec "%%i"
del "%%i"
lame -m m "%%~dpni.wav"
del "%%~dpni.wav"
)
I'm having space issues on my Vista machine and need to figure out what's taking up so much space.
I would like to write a simple batch file that returns all folders under C: and the size of each folder.
The dir command doesn't appear to return folder size.
Unfortunately we don't have admin rights and can't install a third party application and we have other users in our group that also need this information.
I'd have a look at this thread for some clues as to how to achieve the directory size:
Batch File To Display Directory Size
Otherwise:
dirsize:
#echo off
setLocal EnableDelayedExpansion
set /a value=0
set /a sum=0
FOR /R %1 %%I IN (*) DO (
set /a value=%%~zI/1024
set /a sum=!sum!+!value!
)
#echo %CD%:!sum! k
AllDirSize:
echo off
set WORKING_DIRECTORY=%cd%
for /f "delims=" %%a in ('dir /a:D /D /B /S') do (
echo off
cd %%a
"%WORKING_DIRECTORY%"\dirsize "%%a"
cd %WORKING_DIRECTORY%
)
Use it: ALLDIRSIZE > C:\temp\FileContainingFolderSizes.txt
Which is taken from the excellent Richard Bishop testing forums: http://www.bish.co.uk/forum/index.php?topic=58.0
Not exactly answering your question, but if you have GUI access I'd suggest using TreeSize:
http://www.jam-software.com/freeware/index.shtml
If you prefer command line use du command from Unix utils:
http://unxutils.sourceforge.net/