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
Related
I need to copy a file i already created (a.txt) and copy it so i will have
a1.txt
a2.txt
a3.txt
etc.
until i get a999999.txt
then, copy the files that ends with "3", for example a4453.txt to a different folder.
What is the best way to do it? vb/batch file preferred.
Thanks =]
Daniel.
#echo off
set "different_folder=c:\diff"
for /l %%n in (1;1;999999) do (
copy "a.txt" "a%%n.txt"
)
for /f %%f in ('dir /b a*.txt ^| findstr /i /e "3.txt"') do (
copy "%%~ff" %different_folder%
)
Not tested.
We have a number of Apache Tomcat for Windows logs - for example..
server.log.2014-02-04-10
server.log.2014-02-04-11
server.log.2014-02-04-12
server.log.2014-02-05-13
server.log.2014-02-05-14
server.log.2014-02-05-15
These are defined with a file extension of .YYYY-MM-DD-HH and by default Apache has them by date: .YYYY-MM-DD however, we have to define the HH breakover because of system usage and other reasons.
I am trying to do something very simple..
If the file extension does not equal today, to archive (zip) it up..
Here is what I have and no matter what I do is that it shows: 2014-02-05 even though the script is showing .2014-02-04.
Here is the script and I am not sure if I need SETLOCAL enabledelayexpansion..
rem http://www.dostips.com/DtTipsStringManipulation.php#Snippets.Replace
SETLOCAL enabledelayedexpansion
for /f "skip=1" %%x in ('wmic os get localdatetime') do if not defined MyDate set MyDate=%%x
set today=!MyDate:~0,4!-!MyDate:~4,2!-!MyDate:~6,2!
rem today=%MyDate:~0,8%
for %%i in (D:\11\*.*) do (
SET FILETIME=%%~xi
rem SET Dt=!FILETIME:~0,11!
rem IF NOT "!fldt!" == ".!today!" (
rem echo %%~ni%%~xi
rem )
)
pause
Thanks
This filters the filenames by the date string in the name and
will echo the filenames that do not have todays date in them.
#echo off
rem http://www.dostips.com/DtTipsStringManipulation.php#Snippets.Replace
SETLOCAL enabledelayedexpansion
for /f %%x in ('wmic os get localdatetime ^|find "." ') do set "MyDate=%%x"
set "today=!MyDate:~0,4!-!MyDate:~4,2!-!MyDate:~6,2!"
rem set "today=%MyDate:~0,8%"
for /f "delims=" %%a in ('dir "D:\11\*.*" /b ^|find /v ".%today%-" ') do (
echo "%%~fa"
)
pause
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.
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/