I need a batch file that looks in a file and moves a set of files
the problem is, is that it will need to compare files as there may be more than 1 set
the files correspond to each other
the first file in the set is BIAK1234
the second file is BIPO1234
Note the the 1234 is just the file number and will indicate what set it is a part of
So the batch file must find the set of files - BIAK1234 and BIPO1234 and move them to another file
if BIAK1234 does not exist then the script needs to end
But if there are files: BIAK1234, BIAK12345, BIPO 1234, BIPO12345 and BIPO123456
bipo123456 must remain until BIAK123456 exists and then it may be moved over
so how do I compare the files
I already know how to make a batch file move files and how to check to see if a file exists I am just not quite sure how to compare the files, determine the sets and move ONLY the sets
I struggled a bit with your question. At first I thought you were getting the file names from a text file. But your last paragraph led me to believe you are simply dealing with files in a folder.
As you say, the only moderately tricky bit is determining the individual set IDs. The solution is to use the SET command to either get a substring, or else replace the prefix with nothing. Type set /? from the command prompt to get more information.
In the solution below, I opted to use the search and replace method.
#echo off
setlocal enableDelayedExpansion
set "source=c:\yourSourceFolder"
set "destination=c:\yourDestinationFolder"
set "file1=BIAK"
SET "file2=BIPO"
pushd "%source%"
for %%F in ("%file1%*") do (
set "setID=%%F"
set "setID=!setID:%file1%=!"
if exist "%file2%!setID!" move "%%F","%file2%!setID!" "%destination%"
)
popd
Related
I want to generate multiple files similar to the one which I have kept it in some particular location.
For example the file name is "MM_MTL_IM_20180726123109", only last two digits of the file name should keep on changing.
Can we achieve this with batch script or please suggest me any other technique
Here is a basic example script which is designed not for efficiency or to be the best method but to show some elementary batch file commands with their syntax in a logical progression.
#Echo Off
Set "FullFileName=MM_MTL_IM_20180726123109.ext"
If Not Exist "%FullFileName%" Exit /B
For %%A In ("%FullFileName%") Do (
Set "FileNameOnly=%%~nA"
Set "FileExtension=%%~xA"
)
Set "FileNameBarLastTwoChars=%FileNameOnly:~,-2%"
Set "LastTwoFileNameChars=%FileNameOnly:~-2%"
For /L %%A In (100,1,159) Do (
Set "Seconds=%%A"
If Not "%%A"=="1%LastTwoFileNameChars%" (
Call Copy "%FullFileName%" "%FileNameBarLastTwoChars%%%Seconds:~-2%%%FileExtension%"
)
)
Pause
You only requested 'multiple' files with no explanation, so you will need to make adjustments to the code if you don't wish this script to copy your file 59 times so that you have sixty matching files with different names in the same directory, each having valid seconds.Your question also states 'generate' not copy, so you will have to adjust the code if the copy assumption was incorrect.
#echo off
setlocal
set "name=%~1"
set "stem=%name:~,-2%"
for /L %%i in (10,1,59) do copy %name% %stem%%%i
This batchfile takes the original filename as parameter and creates 49 copies of it, appending "10" to "59" instead of the last 2 characters.
In this simple form this will work only if original file and copies are in the same, the current, directory.
I want to create a program that can loop through multiple pdf files and have the user rename each file a unique name like so:
234324.pdf to Batch150.pdf
32154687.pdf to AdvancedPayment.pdf
and so on...
Here is my code:
setlocal EnableDelayedExpansion
echo rename pdf files
FOR %%F IN (*.pdf) DO (
set /p x=Enter:
move %%F !x!
)
endlocal
This seems to work for the first file and then when I try to rename the second one it says: The syntax of the command is incorrect..
I have tried using the rename command and haven't had much luck with it.
FOR %%F IN (*.pdf) DO (
SET "x=%%F"
set /p "x=%%F Enter: "
IF /i "!x!.pdf" neq "%%F" ren "%%F" "!x!.pdf"
)
Your code worked fine for me. Since you don't indicate what the "to" and "from" names you used were, we're reduced to guessing.
I developed the above code to perform the rename in the manner you originated. Note that it works as I expect. Using ren the "to" filename must be a filename within the directory where the "from" file resides [ie. it simply renames the file]. If you use "move" then the file can be moved to another directory if you specify that in the "to" name.
The first fix is purely cosmetic. By enclosing the variablename and prompt in the set /p in quotes, you can include a terminal space in the prompt (which I prefer), and including the %%F in the prompt shows you which file is about to be renamed.
The next fix is to quote the arguments to the ren or move. This ensures the syntax remains properly constructed in the case of eithr "to" or "from" name containing a space.
The next is to initialise x with the "from" filename. It's enclosed in quotes so any invisible trailing spaces are not included in the value assigned to x. Note that set /p does not alter the variable if Enter alone is keyed, so setting x ensures that if a file is not to be renamed, all you need do is press Enter
The next is to detect whether the "to" and "from" names are equal. ren will generate an error report if you attempt to rename a file to itself; equally, you can't move a file to itself. Hence, /i=ignore case, and only attempt the operation if the names are different.
Finally, add the .pdf to each usage of !x! in order that you don't need to key it in. Naturally, you could omit this change if you want to alter extensions or you could put .pdf into another variable and use that variable in place of the constant .pdf so that the extension being selected can be easily varied by being changed in one plaace rather than using a mass-edit. You could even use a set /p to assign the extension being processed dynamically at the start of the routine.
Note that if you rename say one.pdf to yellow.pdf then this construct is very likely to propose yellow.pdf for a rename later on. This is because the next-filename logic locates the next filename currently in the directory,then processes the loop, then locates the next filename currently in the directory, and so on.
You would need
For /f "delims=" %%F in ('dir /b/a-d "*.pdf" ') do (
to ensure that each filename is only presented once. This mechanism performs a directory scan of filenames-only in basic form, and stores the list in memory, then processes the list item-by-item and since that list is created and then processed, any subsequent alterations to the directory do not affect the list.
I am a bit new to DOS batch files, and I am having a hard time wrapping my head around ways to solve my problem.
What I need to do: I have a large, nested source folder structure, let's say it lives here:
C:\dir1\dir2\dir3\file.txt
And I have a mirrored destination structure, although a portion of the root is different:
x:\dirA\dirB\dir1\dir2\dir3\file.txt
My current batch copies all files in the source structure to a destination folder, keeping the folder structure, which is what I want.
The problem:
The intention of my script is to drag a folder from the source structure above onto the bat file, and have it copy the files to the destination. What I want to do, is to allow the user to drag a folder from source dir, let's say /dir2 and all of its subfolders/files onto the batch file, and have it copy the contents over to the SAME spot in the destination structure...
So in this example, the batch file should copy everything in and below:
C:\dir1\dir2\
into
x:\dirA\dirB\dir1\dir2\.
Fortunately (I think) my destination folder structure won't be changing, although the source might be in a different location on each machine. So, if there is a clever way to detect where in the source tree I am, and then replace a portion of the destination path... ???
Here's my simple script so far:
#echo off
set /p FILETYPE="What file type do you want to copy? (For example use txt, max, f3d, or * to copy all files.)"
xcopy %1\*.%FILETYPE% c:\output /s
pause
Thanks so much for any help you guys can give! :)
Ken
UPDATE: Adding updated code sample here because I cannot get my comment to format or allow me enough chars. The stuff below may not be completely correct, I am just trying my best to be clear. I have figured out more since I posted this, basically I need to figur out how (or if possible) to use a string after delims, it only seems to check for each character...
#echo off
rem user drag folder onto .bat (left click, move folder using mouse onto .bat icon)
rem %1 in ex is C:\random_folder\another_folder\proj1\area1\scene23
set destRoot=X:\companyname\allprojects\proj1
set rootDir=proj1
rem assume %1 is folder dragged onto .bat file
for /f "tokens=2* delims=<foldername???>" %%a in ("%1") do (
set part1=%%a
set chunk=%%b
)
set finalDest=destRoot+chunk
xcopy %1\* %finalDest% /E /EXCLUDE:exclusions.txt
pause
I am hoping to create this: "X:\companyname\allprojects\proj1\area1\scene23"
Is the x:\dirA\dirB\ part of the destination folder known? If so, the solution is easy:
#echo off
rem Get current directory, ie: "C:\dir1\dir2"
set curDir=%cd%
rem Eliminate the disk drive, ie: "\dir1\dir2"
set curDir=%curDir:~2%
rem Copy to right destination, ie: "x:\dirA\dirB\dir1\dir2"
xcopy *.%FILETYPE% "x:\dirA\dirB%curDir%"
I hope it helps...
EDIT: Clarification to new comments requested
Ok. There is a source folder and a destination folder. The program above assume that the current folder for the program is source folder, but this may be provided as a parameter if needed. The program achieve the following copy processes:
From source folder -> To destination folder
C:\dir1\dir2 -> x:\dirA\dirB\dir1\dir2
C:\dir1\dir2\dir3 -> x:\dirA\dirB\dir1\dir2\dir3
C:\dir1\dir2\dir3\what\ever -> x:\dirA\dirB\dir1\dir2\dir3\what\ever
C:\XYZ -> x:\dirA\dirB\XYZ
If this is not what you want then explain it as concise as possible (use "files", "folder", "part of name" terms, not "I like", "what if", etc), and include three or four examples.
Also, note that for me drag a folder mean a drag&drop operation taking the source folder pressing the right button of the mouse and leave it over the icon of the Batch file. If you are NOT refering to this operation, please do NOT use "drag" term; use "copy" instead (please also clarify this point).
EDIT: Solution to new requirements
OK! You have not explained correctly before your last example! This problem is just about matching two names: last part of first name must match the same part in second name, and combine first name and the rest of second name. Right?:
First name: X:\companyname\allprojects\proj1
Second name: C:\random_folder\another_folder\proj1\area1\scene23
Result: X:\companyname\allprojects\proj1\area1\scene23
This is the solution:
#echo off
setlocal EnableDelayedExpansion
rem user drag folder onto .bat (left click, move folder using mouse onto .bat icon)
rem %1 in ex is C:\random_folder\another_folder\proj1\area1\scene23
set sourceFolder=%~1
set destRoot=X:\companyname\allprojects\proj1
rem Get last part of First name (destRoot) ie: lastPart=proj1
for %%a in (%destRoot%) do set lastPart=%%~Na
rem Delete from beginning of second name until that part, ie: result=\area1\scene23
set result=!sourceFolder:*%lastPart%=!
rem And insert the First name before the rest
set result=%destRoot%%result%
Or, combining the three steps in just one line:
#echo off
rem user drag folder onto .bat (left click, move folder using mouse onto .bat icon)
rem %1 in ex is C:\random_folder\another_folder\proj1\area1\scene23
set sourceFolder=%~1
set destRoot=X:\companyname\allprojects\proj1
for %%a in (%destRoot%) do set result=%destRoot%%sourceFolder:*%%~Na=%
Please note that if destRoot may contain spaces, it must be enclosed in quotes in the FOR command: for %%a in ("%destRoot%") do ...
Got this on another forum, seems to contain the answr I was looking for, sharing here for others:
#echo off
REM YOU NEED THIS
setlocal enabledelayedexpansion
REM If the passed parameter string has any spaces you must quote it
REM we unquote it using ~
set PassedString=%~1
REM May as well do the same with string to split on
set DelimString=%~2
REM The delim string must appear exactly ONCE in the passed string
REM Replace the delim string with a symbol which will not be in the
REM path to be split, in this example I use #
set splitsub=#
call set tempstring=!PassedString:%DelimString%=%splitsub%!
REM Use FOR to split the modified string
for /f "tokens=1* delims=%splitsub%" %%A in ("%tempstring%") do set part1=%%A & set part2=%%B
REM Show that the 2 variables contain the desired substrings
echo Passed = %PassedString%
echo Part1 = %part1%
echo Part2 = %part2%
Example output:
C:\>splitstring.bat "D:\Backup\mystuff\programming\Visual Basic\project\ABCDE\DEF" "project"
Passed = D:\Backup\mystuff\programming\Visual Basic\project\ABCDE\DEF
Part1 = D:\Backup\mystuff\programming\Visual Basic\
Part2 = \ABCDE\DEF
C:\>splitstring.bat "D:\Backup\mystuff\programming\Visual Basic\project\ABCDE\DEF" "mystuff"
Passed = D:\Backup\mystuff\programming\Visual Basic\project\ABCDE\DEF
Part1 = D:\Backup\
Part2 = \programming\Visual Basic\project\ABCDE\DEF
I'm trying to create a batch file (via Windows XP Pro) that copies 2 files (.zpl) who's filename's vary in length. ZPL files relate to label printer code. File names are as follows:
FillXferDataPBHAMFill###########.zpl
FillFormatsPBHAMFill############.zpl
The pound signs represent a number associated with a particular label/job to be printed. These numbers are identical per job. From one job to the next the numbers vary in length and always change. The directory I'm trying to pull these from contains ZPL files from multiple locations, however, I just want the BHAM ones.
The batch will copy from: \Server\C:\Directory1\Directory2\Directory3
To be copied to: \Server\Directory1\Directory2
Not sure if this will complicate things further, but the batch file will be ran from a 3rd machine. Furthermore, I do not need to copy every file everytime. Whenever new print jobs are sent, supervisors will run the batch to copy the new print jobs within the last X amount of time. X being minutes. Here is what I have so far...
#echo off
SETLOCAL enableExtensions enableDelayedExpansion
SET sourceDir=Server\C:\Directory1\Directory2\Directory3
SET targetDir=Server\Directory1\Directory2
FOR %%a (FillFormatsPBHAM*.bat) DO (
SET "filename=%%a"
SET "folder=%targetDir%"
XCOPY "%%a" !folder!
)
FOR %%b (FillXferDataPBHAM*.bat) DO (
SET "filename=%%b"
SET "folder=%targetDir%"
XCOPY "%%b" !folder!
)
:END
I apologize for a lengthy post; just wanting to be as thorough as possible. I'm learning this on the fly so bare with any ignorance on my part. Thank you in advance for ANY help!!
StackOverFlow Material Reviewed: Reference1, Reference2 -- I've been looking everywhere over the past week and these were the 2 most helpful so far.
I see some ways to fix or improve your BAT script.
The FOR command syntax is FOR %%a IN (*.bat) DO (
The sourcedir variable is set Server\C:\Directory1\Directory2\Directory3, which is not a correct path in Windows.
you initialize but don't use %sourcedir% variabe neither in your FOR loop nor in your copy commnand
you should either change the current drive and dir with a pushd %sourcedir% command, or specifying it in the FOR command.
your FOR loop assigns a %filename% variable that is never used, you may skip this assignment.
you FOR loop assigns a %folder% variable that is only then used in the copy command, you can skip this assignment and simply use %targetdir%
but, to just copy all files from one folder to the other you don't need FOR to iterate over all of them, you might just copy them right.
So, take a look at this simple script to get you started..
SET sourceDir=\\servername\sharename\Directory1\Directory2\Directory3
SET targetDir=\\anotherserver\sharename\Directory1\Directory2
xcopy %sourceDir%\FillFormatsPBHAM*.bat %targetDir%
xcopy %sourceDir%\FillXferDataPBHAM*.bat %targetDir%
Here is the situation, I have directories that have 10,000 files in each directory named like this:
FileName.XX0000.csv
FileName.XX9999.csv
The Size of the filename itself can change ie:
FileName.XXX0000.csv
FileName.XXX9999.csv
FileName.XXXX0000.csv
FileName.XXXX9999.csv
Where the X's are yet another parent number, but these ones should be ignored anyway.
Regardless though all directories are supposed to have exactly 10,000 files no more and no less. The trouble is some are missing and I need to know which ones are missing.
I tried the scripts in this post:
batch script to find missing sequence no
I even messed around with the string sequnce for several hours to no avail. Neither of these options works very well the 2nd script comes close but ends up in an endless loop, or spits out files that do exist.
I need to come up with a solution that will easily tell me which numbers or file names are missing. In my Dream world I would love a script where I can place it inside a parent directory and it would crawl through all the sub directories and print a single file of the missing ones.
It would be nice to not have to tell the script what the beginning part of the file name is, since in theory it should only be looking to see if the last four numbers are in existance from 0000-9999.
Since this is my first post I am hoping I doing this correctly by creating a new topic, I was going to ADD my question to the previous topic until I saw the "But avoid …" in the post box!
Let's see if you can write some code.
FOR /L can count from 0 to 9999
IF NOT EXIST can test if a file is missing, and it supports wild cards.
> and >> can redirect output to a file
For your dream world:
FOR /D /R %%F IN (.) will iterate an entire folder hierarchy
Edit
You probably will need to left pad a number with zeros so that the width is always 4 digits. Since this will probably be within a loop, you will want to use delayed expansion. Assume %%N is a FOR variable that contains a number.
set num=000%%N
set num=!num:~-4!
Here is a script that will find all the missing files in a continuous sequence:
#echo off
setlocal ENABLEDELAYEDEXPANSION
set max=1208
set cnt=0
for /L %%f in (0,1,%max%) do (
set "num=00000%%f"
set "num=!num:~-4!"
if not exist "img!num!.png" (
echo img!num!.png
set /A "cnt=!cnt!+1"
)
)
echo NUMBER MISSING: !cnt!
endlocal
Notice inside the loop you need to use !x! instead of %x% to get a local delayed variable.