I have created a batch script to take a file, copy it to a folder with the date and time as the folder name and then to rename the file also based off the date and time. My issue is that any time before 10 AM, the command fails:
c:\ICVERIFY\ICWin420\DATADIR>md C:\SettlementReports\Settlement_Reports\DATADIR\
06-25-2014_ 709_08.24.txt
A subdirectory or file C:\SettlementReports\Settlement_Reports\DATADIR\06-25-201
4_ already exists.
Error occurred while processing: C:\SettlementReports\Settlement_Reports\DATADIR
\06-25-2014_.
Here is my batch file, I have put a time out right before it errors out.
cd c:\
cd icverify
cd icwin420
cd DATADIR
:: this is Regional settings dependent so tweak this according your current settings
Echo %DATE% %Time%
Set TDate=%date:~10,4%%date:~4,2%%date:~7,2% FOR %%V IN (%1) DO Rename %%V %%V%TDate%
md C:\SettlementReports\Settlement_Reports\DATADIR\%date:~4,2%-%date:~7,2%-%date:~10,4%_%time:~0,2%%time:~3,2%_%time:~6,5%.txt
timeout /T 1
copy ICRPT*.txt C:\SettlementReports\Settlement_Reports\DATADIR\%date:~4,2%-%date:~7,2%-%date:~10,4%_%time:~0,2%%time:~3,2%_%time:~6,5%.txt
cd c:\
cd Settlementreports
cd settlement_reports
cd datadir
cd %date:~4,2%-%date:~7,2%-%date:~10,4%_%time:~0,2%%time:~3,2%_%time:~6,5%.txt
ren ICRPT*.TXT ICRPT_%date:~4,2%-%date:~7,2%-%date:~10,4%_%time:~0,2%%time:~3,2%_%time:~6,5%.txt
Any help is greatly appreciated.
Your %time% variable contains a space instead of an initial 0 left padding the hours for hours less than 10:00. You can use
set "$time=%time: =0%"
to replace the space with a 0, storing the resulting value in a new variable. Then replace the references to %time% with the new variable %$time% as
md C:\ ....... \%date: ... %_%$time:~0,2%%$time:~3,2%_%$time:~6,5%.txt
Or instead, if the space is not a problem in your file names, quote all the file references to avoid the errors
md "C:\Settlem .... _%time:~6,5%.txt"
For any of the two options, do the change in all the commands in your file referencing files/folders
replacing the space with a 0 (see MC ND's answer) is one possibility.
But you should get used to put any <path>\file.ext into doublequotes.
copy file.txt new path\file with spaces.txt
is interpreded as copy file.txt to new, and there are additional parameters path\file, with and spaces.txt, which are not expected.
This syntax works better:
copy "file.txt" "new path\file with spaces.txt"
Here there are only two parameters, each enclosed in doublequotes.
Related
Ok, i've been working on a batch file for some time now, and im just stuck on the last bit.
What im trying to accomplish is to loop through a directory, create a variable which stores the filename of each file in the directory without the extension. Then for each file in the first loop, loop through a different directory and try to find any filename in the second loop that has the same name as stored in the variable, and then just output some simple text.
So for instance lets say in the first directory there is a filename called imafile-yehyeh.png, the variable will save imafile-yehyeh, then it will loop through all the files in the second directory, and output a message for each filename that has that pattern in it, so if a file in the second directory is called imafile-yehyeh_01.mp4 or imafile-yehyeh-newtitle.jpg, they would match the pattern and a message would output.
My script is looping and i am able to echo out all the variables, the files exist as i have created them exactly as shown above, but its not echoing out the filename is set for deletion line.
Any help would be greatly appreciated. my code is as follows;
#echo off
set "parent_folder=C:\Users\Testing\script"
set "dupe_folder=DUPEFOLDER"
set "kill_folder=1 SCANNED\thumb"
setlocal enableDelayedExpansion
for %%X in ("%parent_folder%\%dupe_folder%\*") do (
set dupe_pattern=%%~nX
for %%F in ("%parent_folder%\%kill_folder%\*") do (
echo %%~nF | FIND "%dupe_pattern%" 1>NUL && (
echo %%~F is set for deletion.
)
)
)
endlocal
Thanks to #Squashman the answer was to remove the set dupe_pattern.... line
and then change the FIND command to the following;
FIND "%%~nX"
Apart from needlessly setting a variable, as already pointed out, you are also making the script inefficient. For every file in the dupe_folder you are Echoing every file name in the kill_folder and piping that into a Find command looking for matches.
Here's a simpler way of doing it, (it matches file names which begin with the same string followed by a dot, as opposed to any file name containing the string anywhere).
#Echo Off
Set "parent_folder=C:\Users\Testing\script"
Set "dupe_folder=DUPEFOLDER"
Set "kill_folder=1 SCANNED\thumb"
CD /D "%parent_folder%" 2>Nul || Exit /B
For %%A In ("%kill_folder%\*") Do If Exist "%dupe_folder%\%%~nA.*" (
Echo %%A is set for deletion.)
I have a bunch of .flv files in subdirs and I need to loop through them and rename it according to its path.
I was able to loop through them all, but I don't know how to split the path and rename it using batch script.
Here's what I have so far:
echo off
for /R %%F in (*.flv) do (
echo %%~pF
)
The "echo %%~pF" prints the path for the current file on the loop, something like this:
\folder\morefolders\activity\ NameThatIwant \Videos\
I tried spliting with "delims=\" on my for loop but I get only "echo off".
I tried other tutorials, read other questions on SO but none of them were renaming the files from a split string from the path of the file in the loop.
Could you guys help giving suggestions or direct me to any material that explains those %% codes?
Thanks.
I think you do not need to split the path, though you could do it using for /f "delims=NameInthePath tokens=1", where NameInthePath - is some word in the path and tokens= gives you the first part of the path separated by delims.
Actially, if you need to rename file name you need to use REN command. If you need to change the path for the flv file - use copy of move command.
A batch file to rename all .LOG files to .TXT in the 'demo' folder and all sub-folders:
CD C:\demo\
For /R %%G in (*.LOG) do REN "%%G" "%~nG.TXT"
I have identical files in four folders (named "1", "2", "3", "4") and would like to copy these files into a single folder, with the original folder name appended to the filename.
E.g. a file called "data.txt" in each folder should be copied to a new merged folder, with filenames such as "data 1.txt" "data 2.txt" etc.
Here is what I have so far, but I never formally learned batch scripting (and can't find any decent tutorials - recommendations please?) and can't seem to make it work. Hopefully this gives an idea of what I want to accomplish.
DIR="$( dirname "$0" )" && pwd )" // I don't understand this but was told it's
// necessary to set the working directory as
// the current folder? Is that correct?
md "consolidated files"
for %%i in ("1","2","3","4") do
copy *.txt '../consolidated files/"*"+%%i.txt'
Any tips for a beginner? Thanks!
#ECHO OFF
SETLOCAL
PUSHD "U:\sourcedir"
MD "consolidated files" 2>nul
for %%i in ("1","2","3","4") DO (
FOR /f "delims=" %%m IN ('dir /b /a-d ".\%%~i\*.txt"') DO (
copy ".\%%~i\%%m" ".\consolidated files\%%~nm %%~i%%~xm"
)
)
popd
GOTO :EOF
Your attempt to set dir appears to be a bash command - useful on *nix but no good on CMD.
Essentially, you can set the current directory using cd
cd "c:\your\desired directory"
Quirkishly, the quotes in that particular command are actually unnecessary (but do no harm, so I put them in.)
Another approach is
pushd "c:\your\desired directory"
rem commands following have current directory "c:\your\desired directory"
rem
popd
rem current directory reestored to value before the "pushd"
I've used the second approach in the above script to switch temporarily to my test directory U:\sourcedir
Note that cmd uses \ as a directory-separator and / as a switch-indicator.
The md command is as you had it. The directory is created relative to the current directory unless the path is specified (eg md "C:\somewhere new"). The2>nulsuppresses thedirectory already exists` message should the directory er, already exist.
in a for...do statement, either the target operation must be on the same line as the do or the do must be followed by Space( and then each statement until a matching ) is executed as a compound statement.
The for..%%i statement assigns the values "1".."4" (including the quotes) to %%i The quotes are actually not required in this case - they only need to be there if the required string includes a Space (or other separator.)
The next command is best understood from the middle. The dir command looks in ".\%%~i\" for files named *.txt. ~i means "remove quotes from %%i". The /b switch shows just filenames - no size, date or header/footer. The /a-d switch says 'no directories'.
This dir command is within single-quotes. FOR /f ...('single-quoted command')... processes the result of the command as if it was a file, line-by-line. The "delims=" suppresses the default tokenising of the strings found, so overall, the filenames found by the dir are assigned to %%m in their entirity.
The command then executed is the copy, copying from ".\%%~i\%%m" (ie. the current directory++the subdirectory(-quotes)++filename; all quoted in case of spaces) to ".\consolidated files\%%~nm %%~i%%~xm" (ie. the current directory+\consolidated files+the name part of the filename (%%~nm)+Space+the subdirectory(-quotes)+the extension part of the filename (%%~xm))
Note that + is a valid filename character (as is ') and that strings are built simply by being butted up one against the next.
Your original question stated that the source directoryname should be appended after a space, hence I've included the space.
Note that copy will report 1 file(s) copied after each copy. You can suppress this by adding >nul to the end of the copy statement.
For testing, I would change copy to echo copy which will show the command generated but not execute it. Unfortunately, if you have the >nul in place, the echo of the command will be suppressed...
I am very new to coding and bulk processes but i am looking for a command line SPECIFICALLY for windows command prompt and i am wondering if such a thing exists. So I have a folder containing 111 subfolders, with each subfolder containing between 20 and 40 png image files. Each subfolder is named 001-111 accordingly and the png files are ordered how i want them, however i am looking for a command line that would be able to quickly and efficiently name all the pngs in the folders to the name of the folder followed by the png number in brackets
e.g. for folder 037, i would want the png's to be renamed to: 037(1), 037(2), 037(3) etc...
I am hoping for the best although i am unsure such a code may not be possible or be simply done.
Also if you come up with a code that achieves this process, it would be great if you could reply with the simple command line that i could use rather than a full explanation because i am new to coding and far from fluent with the language or terms or how things work. I know this same process can be achieved by going select all>rename (ctrl a>f2) and renaming to the folder name however i need to use this process frequently and dont want to have to open each folder, i would rather have a command line for cmd that would do it swiftly
Thank you and a simple answer would be greatly appreciated
#ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
SET "parentdir=u:\parent"
FOR /l %%a IN (1001,1,1111) DO (
SET dir=%%a&SET "dir=!dir:~1!"
FOR /f "delims=" %%i IN ('dir /a-d /b "%parentdir%\!dir!\*.png" 2^>nul') DO (
ECHO REN "%parentdir%\!dir!\%%~nxi" "!dir!(%%~ni)%%~xi"
)
)
GOTO :EOF
Test results:
Starting directory :
u:\parent\001\1.png
u:\parent\037\1.png
u:\parent\037\2.png
u:\parent\111\999 with spaces in name.png
Script response
REN "u:\parent\001\1.png" "001(1).png"
REN "u:\parent\037\1.png" "037(1).png"
REN "u:\parent\037\2.png" "037(2).png"
REN "u:\parent\111\999 with spaces in name.png" "111(999 with spaces in name).png"
Obviously, you'd need to replace the value assigned to parentdir with your actual target directory name.
The script will report the renames it proposes to do. To actually invoke the rename remove the ECHO keyword.
I would create a batch file like so:
renamepng.bat:
cd %%1
if ERRORLEVEL 1 goto end
for %f in *.png do mv "%f" "%%1(%f).png"
cd ..
:end
This will attempt to cd to the directory name provided on the command line, abort if that fails, then rename all the .png files and return to the previous directory
then call it like so:
for %d in ??? do call renamepng.bat %d
which will loop through all 3-character file and directory names in the current directory, can call the batch file on each one. Using call instead of just the batch file name causes execution to return to the loop when the batch finishes.
I'm writing a shell script to backup the contents of the current directory to a timestamped subdirectory, ./STATES/$DATE. Obviously I do not want to STATES directory itself to be re-copied each time I backup the folder, so I need to somehow exclude it from the copy.
Here's an untested shell script showing how I'd approach this on *nix:
ID="$(date +%Y%b%d%H%M%S)"
COMMITABLE="$(ls | egrep --invert-match ^STATES\$)"
STATE_PATH="$(pwd)/STATES/$ID"
mkdir --parents "$STATE_PATH"
cp $COMMITABLE "$STATE_PATH"
ln -s "$STATE_PATH" PARENT
How could I achieve this in a batch file?
Here you go. This is from my own scripts:
set now=%date:~-4%-%date:~-10,2%-%date:~-7,2%-%time:~0,2%-%time:~3,2%-%time:~6,2%
rem Before 10:00 o'clock a space is used, this makes it a zero.
set now=%now: =0%
xcopy . copydir-%now% /i
One word of warning: this uses the US date format. For matching a different format you will have to change it. As I work with Dutch and US systems I personally use this code:
rem US date versus Dutch: test 5th char is /
if "%date:~-5,1%"=="/" (
rem Date format is mm/dd/yyyy
set now=%date:~-4%-%date:~-10,2%-%date:~-7,2%-%time:~0,2%-%time:~3,2%-%time:~6,2%
) else (
rem Date format is dd-mm-yyyy
set now=%date:~-4%-%date:~-7,2%-%date:~-10,2%-%time:~0,2%-%time:~3,2%-%time:~6,2%
)
xcopy's /exclude:$FILE option can be used to specify a file containing filenames to be ignored. A friend of mine converted the script to batch like this:
#echo off
set thedate=%date:~0,2%-%date:~3,2%-%date:~6,4%
md %thedate%
echo STATES > excludefile.txt
echo excludefile.txt >> excludefile.txt
xcopy <root folder of directory structure> %thedate% /e /i /exclude:excludefile.txt
del excludefile.txt