I have very limited knowledge of DOS commands (mainly simple move/copy/del/rename commands) and I would like some assistance in creating a batch file that does the following steps:
Prompts the user to enter in a Version Number in an input box.
Validates the user's input to ensure that it is entered as major version, hyphen, minor version (e.g 5-10)
Searches the current folder where the batch file is being run from and renames all PDF's by appending the version number and a hard-coded description to that file.
For example, an original file of EMDM.pdf, should be renamed as EMDM_5-10_Software Operations Manual.pdf (note the underscore before and after the version number, and spaces in the description text)
Goes to \webserver\downloads and 'MOVES' the PDF file in that location that starts with "EMDM" and ends with "Software Operations Manual.pdf" to \webserver\downloads\supserseded
Once the previous version PDF has been moved (backed up), 'COPY' the newly renamed PDF that exists in the same folder as the bacth file to \webserver\downloads
Once successfully, moved, delete the PDF file that exists in the same folder as the bacth file.
Thank you in advanced.
#echo off
:getversion
REM 1.
set /p VersionNumber=Enter the Version Number:
REM 2.
for /f "tokens=1-3 delims=-" %%a in ("%VersionNumber%") do set Major=%%a& set Minor=%%b
REM 2.1 Revision of Version Number format
if not "%Major%-%Minor%" == "%VersionNumber%" goto getversion
REM 2.2 Revision of Major and Minor be numbers
set /a NMajor=Major, NMinor=Minor > NUL
if not "%NMajor%" == "%Major%" goto getversion
if not "%NMinor%" == "%Minor%" goto getversion
REM 3.
for %%a in (*.PDF) do ren "%%a" "%%~Na_%VersionNumber%_Software Operations Manual.pdf"
REM 4.
pushd \webserver\downloads
move "EMDM*Software Operations Manual.pdf" supserseded
REM 5.
popd
copy "EMDM*Software Operations Manual.pdf" \webserver\downloads
REM 6.
del "EMDM*Software Operations Manual.pdf"
REM Steps 5 and 6 above is the same as just one MOVE
I modified the revision of the Version Number by an easier method.
Related
I am creating a batch file to be run later which will be used to copy files from one location to another while renaming the files, skipping any existing files, and without prompting the user. Examples of files to be copied:
00021001.txt
00021001.xyz
00021001.abc
00021001001.jpg
Copied files will have the names:
00022001.txt
00022001.xyz
00022001.abc
00022001001.jpg
Things I have tried:
xcopy C:\Testing\1000012\21\00021*.* C:\Testing\1000013\22\00022*.* /D
This almost does it. It copies all the files starting with "00021" in the first location into the second location while properly renaming them to start with "00022". It skips all the files with the same name and date stamp, but ends up prompting to copy any files from the source which are newer than the target.
robocopy C:\Testing\1000012\21\ C:\Testing\1000013\22\ 00021*.* /xo /xn /xc
I was hoping that by excluding older, newer, and same date files it would work (even if it doesn't rename - I would just do that in a separate step.) Unfortunately, this just ends up overwriting newer source files over existing target files if they are a different filesize.
I have even tried the Copy-Item command in PowerShell. But it doesn't do the renaming like Xcopy, and it doesn't skip existing files (although I can get it to confirm and say "No to All".)
Copy-Item -Path "C:\CWUImageCompare\Testing\1000012\CWU\chemistry\129\21\00021*.*" -Destination "C:\CWUImageCompare\Testing\1000013\CWU\chemistry\129\20\" -Confirm
If xcopy had the "Skip if existing" flag I'd be all set, but it doesn't.
#ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
rem The following settings for the source directory & destination directory are names
rem that I use for testing and deliberately includes spaces to make sure
rem that the process works using such names. These will need to be changed to suit your situation.
SET "sourcedir=u:\your files"
SET "destdir=u:\your results"
FOR /f "skip=1delims=" %%b IN ('xcopy /L /Y "%sourcedir%\00021*.*" "%destdir%\00022*.*" ^|sort') DO (
SET "oname=%%~nxb"
IF EXIST "%destdir%\00022!oname:~5!" (ECHO "%%b" skipped) ELSE (ECHO COPY /y "%%b" "%destdir%\00022!oname:~5!")
)
GOTO :EOF
Always verify against a test directory before applying to real data.
Seems a little complicated, but essentially, execute the xcopy (I omitted the /D for testing) with /L /Y to simply produce a list.
Since the list has a last line that begins with a numeric, whereas the other lines start with a drive-letter, sort the list and skip the first line.
This would implement the date-requirement.
Then grab the part after the first 5 characters of the name+extension, test whether the new name exists and either report or copy as appropriate (The copy command is disarmed for testing)
I have a daily process which generates some zip files out of files that are being created by other processes. I need to create a daily log file which indicates the timestamps the contents of one specific file of each zip file that is found.
I created the following batch script which seemed to work yesterday on my test system, but not anymore today, no idea why...
set VersionDirectory=C:\Test\VersionX\
set ResultOutputFile=C:\Test\LogFile.txt
for /f %%f in ('dir /b %VersionDirectory%\Installable\Packages\pattern*.zip') do (
mkdir %temp%\%%f\
unzip -extract -dir %VersionDirectory%\Installable\Packages\%%f %temp%\%%f\ > nul
for %%a in (%temp%\%%f\InstallScript.xml) do set InstallScriptXMLDate=%%~ta
rmdir /s /q %temp%\%%f\
echo %%f [package from %InstallScriptXMLDate%] >> %ResultOutputFile%
)
Short summary of what this file is supposed to do:
Loop through each pattern*.zip file in C:\Test\VersionX\ directory
unzip this file to the %temp%\%%f directory (where %%f is the filename)
Get the timestamp of the %temp%\%%f\InstallScript.xml and put it in the %InstallScriptXMLDate% variable
Delete the %temp%\%%f directory
Echo the filename (%%f) and timestamp (%InstallScriptXMLDate%) into the log file
As of now the log file just contains the filenames, followed by the string '[package from ]' string, but missing the actual date timestamp
The unzipping and removing of the zip files is working flawlessly, it's just the timestamp that's not being set.
You are setting a variable and using it in the same block. This cannot work in cmd because environment variables are expanded when a statement is parsed not when it's executed. So when the loop is run all variables have already been replaced with the values they had before the loop.
Put
setlocal enabledelayedexpansion
at the start of your batch and use !InstallScriptXmlDate! instead of %InstallScriptXmlDate%.
Another note: for is perfectly capable of iterating over files by itself, you almost never need to iterate over dir /b output with for /f. In fact, it can introduce problems that can be avoided with
for %%f in (%VersionDirectory%\Installable\Packages\pattern*.zip)
I was wondering if it is possible to count have many files are in a directory, divide the number of files by 3 and then ftp the files to 3 seprate folders on a web server?r 2
EX. If I have 21 files in a folder, I need the script to find out how many files are in there, then divide by 3. I then need to FTP first 7 to folder1 on ftp server, upload files 8-14 to folder number 2, and upload the last 7 files to folder number 3.
Any help would be greatly appreciated.
The Windows Batch file below do what you want in a local (same computer) folder. You may adjust the details for this to work over a network.
#echo off
rem Following line is required to use !var! value into FOR loops:
setlocal EnableDelayedExpansion
rem Count the files:
set fileCount=0
for %%f in (*.*) do set /A fileCount+=1
rem Copy files to folder!folder!; increment folder every filesPerFolder=fileCount/3
set /A filesPerFolder=fileCount/3
set folder=1
set i=0
for %%f in (*.*) do (
copy %%f folder!folder!
set /A i+=1
if !i! == %filesPerFolder% set /A folder+=1, i=0
)
I would like to write a batch file containing DOS commands (unfortunately perl or another language is not an option) to do the following task.
In one directory (c:\MyData\Directory1) there are the following files:
File2.txt
File2.dat
FileA.bin
FileQ.bin
FileC.bin
File8.bin
File2.bin
These files all have different creation dates. The most recently created *.bin file is File2.bin in this example, but it could be any randomly named *.bin file.
In another directory (c:\MyData\Directory2) there are the following files:
File2.txt
File2.dat
FileA.bin
FileQ.bin
Here is what I want to do:
Copy all files with the extension *.bin in Directory1 that do not already exist in Directory2 except for the most recently created *.bin file in Directory1. So the only files that should be copied into Directory2 are:
FileC.bin - Copy because it's a bin file that's not yet in Directory2
File8.bin - Copy because it's a bin file that's not yet in Directory2
The following files should not be copied into Directory2:
File2.txt - Wrong extension so don't copy it
File2.dat - Wrong extension so don't copy it
FileA.bin - Already exists in Directory2 so don't copy it
FileQ.bin - Already exists in Directory2 so don't copy it
File2.bin - Most recent *.bin file so don't copy it
Thanks for any help!
#echo off
#rem Sorry for excessive commenting - I am a batch file newbie
#rem Batch file will not work if there are spaces in names of directory or copied files
#rem Next line allows for/do loop to work correctly
setlocal enabledelayedexpansion
#rem Make temporary file that lists files from newest to oldest
DIR /o-d /b c:\temp\Directory1\*.bin > FileList.txt
#rem Counter will be used to avoid copying newest file which is listed first
set /A Counter=1
#rem Read in names of all files with chosen extension in the first directory
#rem Names will be stored in the variable %%a
for /F "delims=" %%a in (C:\temp\FileList.txt) do (
#rem Increment the counter
set /A Counter+=1
#rem Only copy files that are not the most recent one, so Counter>1
#rem Requires the exclamation points because this is a string not number comparison
if !Counter! gtr 1 (
#rem If the file does not already exist in Directory2, copy it
if not exist C:\temp\Directory2\%%a (
echo Copying C:\temp\Directory1\%%a to C:\temp\Directory2\%%a
copy C:\temp\Directory1\%%a C:\temp\Directory2\%%a
)
)
)
#rem Remove the temporary file
del FileList.txt
You can use DIR *.bin /o-d /b > Files.txt to get a list of bin files ordered most recent to last. Do this on both folders (to separate output files), and then set up a FOR loop, maybe two nested FOR loops, to go through the two files, pick out the ones to copy (with special handling for the first one in the date-ordered list), and copy them from within the loop. Silly tricks would be done with setting the Attribute setting and then using XCOPY /M to copy them all at the same time, but that seems overly fussy.
I've always found FOR loops to be cantankerous beasts, and if you can find a non-batch-file way, or some form of third-party plug in to help, you'd be ahead of the game.
I don't have Robocopy on my machine, otherwise I would do a /? and tell you. But as I recall it has many more capabilities (especially wrt timestamps). It's a windows tool.
http://en.wikipedia.org/wiki/Robocopy
how get the date and time of the last modified particular TYPE file in that directory
let me explain with an example
if i use the command dir *.reo /o:d
i get the all *.reo files in that directory sorted according to the date ..
this is the the last line of the output
29-03-2010 11.31 arun.reo
now i just want to copy the date and time of the last created file in variable or file .is it possible ?
You can do this using a batch file like this:
#echo off
setlocal enableextensions
for /f "delims=" %%f in ('dir *.reo /o:-d /b') do (
set dt=%%~tf
goto endloop
)
: endloop
echo %dt%
A little explanation:
dir *.reo /o:-d /b produces a list of all .reo files in the current directory, sorted by date in descending order (so that the last modified file comes first).
%%~tf expands to the date of the file specified by the %f variable.
It depends on what os your using. If you want just the last file
command one pipes the complete directory into a temp file
dir *.reo /o:d > temp.txt;
command two gets the last line of the temp file. Only works if you have the windowserver 2003 install which the link is provided below.
tail - 1 temp.txt;
Go to the Microsoft Windows Server 2003 download section at http://www.microsoft.com/windowsserver2003/
downloads/tools/default.mspx. Or, if that link does not work, visit http://www.microsoft.com/ and search for "Windows 2003". Once there, choose the "Downloads -> Tools" link.