I wrote this .BAT script, pulling snippets from all over stackoverflow about 2 years ago.
:BOF
#echo off
set upper=
set /p CDNUM=Enter NEW CD number:
set superpath=f:\prints\"CD Numbers"
for /f "skip=2 delims=" %%I in ('tree "\%CDNUM%"') do if not defined upper set "upper=%%~I"
set "upper=%upper:~3%"
ECHO.%upper% | FIND /I "CD">Nul && ( goto:Found ) || ( goto:notfind )
:Found
if exist %superpath%\17000s\%upper%\ goto ALLMADE
ECHO.%upper% | FIND /I "REV">Nul && ( goto:NEWREV ) || ( goto:norev )
:norev
md %superpath%\17000s\%upper%\"Rev -"\Cust
set ultrapath=%superpath%\17000s\%upper%\"Rev -"
copy %superpath%\START.dxf %ultrapath%
ren %ultrapath%\START.dxf "%upper% ASSEMBLY.dxf"
echo %upper% has been successfully created.
PAUSE
goto:eof
:ALLMADE
echo That CD number already exists!
PAUSE
goto:bof
:notfind
set upper="CD%upper%"
goto:Found
:NEWREV
echo Revision's not yet supported!
PAUSE
goto:bof
It prompts the user for a 'CD NUMBER', checks to make sure the folder does not already exist, creates it along with some sub directories, then copies and renames a .dxf file.
Currently the script can see if the folder already exists, returning an error message. And if only a number is given it adds a 'CD' to it, as well as making them all caps.
It's been so long since I put this together, that when I look at it I no longer remember how exactly some parts of the script work or what they do.
What I would like to add to the script is the following:
Return an error if the input is less than 5 numbers or does not begin with '17*', 'cd17*', or 'CD17*'.
If the input includes a 'REV' (ex. rev b), change the Rev directory accordingly. Add REV B to the filename of the .dxf (ex. CD17003 REV B ASSEMBLY)
Common sense error checking...
This script is amazing for what it does right now, I'm just trying to improve it. Any help is greatly appreciated.
Related
I'm trying to create a batch file to insert a string from a .txt file at a specific place inside a string in 225 batch files - i.e., inserted into one line in the file at a specific place - but this question concerns the inserting part, and not the loop part, so I've left out the latter in my code example. It's also currently just displaying the text on-screen; not actually writing it to files.
The target files are a bunch of launch .bat files used for running a game server cluster using a tool, so I will have to leave each of them with the same names as they start with (Start XXYY.bat). They contain something along these lines:
start /high ShooterGame\Binaries\Win64\ShooterGameServer.exe Ocean?ServerX=0?ServerY=0?AltSaveDirectoryName=0000?ServerAdminPassword=1234?MaxPlayers=50?ReservedPlayerSlots=25?QueryPort=50002?Port=5002?SeamlessIP=192.168.1.225?RCONEnabled=true?RCONPort=28450 -log -server -NoBattlEye
exit
Where the ServerX, ServerY, AltSaveDirectoryNamen and all three Port settings are unique to each server, so these will have to remain unchanged.
I need to add several more settings, from another .txt file in the final version, but for this example I will just put the additions (the word INSERT added after the ReservedPlayerSlots setting, while keeping each setting divided by question marks) directly into this script.
My code is actually doing exactly what I want it to, but unfortunately it doesn't stop at that point, and decides to append more text than I wanted; specifically, everything I add to the ECHO command which is not a variable name.
To clarify, I get the exact output that I want... Plus the unwanted addition of a bunch of question marks and the word INSERT, which apparently come from my ECHO command, but I just have no idea why they get re-added.
My knowledge of batch scripting is fairly limited, so there might well be something basic that I've overlooked.
I've tried replacing the question marks in the output (which are required to be questions marks in the final version) with normal letters instead, but it doesn't change the behaviour; they were still appended to the expected output, just like the question marks they replaced.
#ECHO OFF
SET FileNum=0000
REM I will have the code loop through 225 files (0000-1414) in the final version, but for test purposes I just set it to one single file number manually here.
SET FileName=Start %FileNum%.bat
REN "%FileName%" temp.txt
FOR /F "tokens=1,2,3,4,5,6,7,8,9,10,11,12 delims=?" %%a IN (temp.txt) DO (
ECHO %%a?%%b?%%c?%%d?%%e?%%f?%%g?INSERT?%%h?%%i?%%j?%%k?%%l
)
REN temp.txt "%FileName%"
I expect this code to output this:
start /high ShooterGame\Binaries\Win64\ShooterGameServer.exe Ocean?ServerX=0?ServerY=0?AltSaveDirectoryName=0000?ServerAdminPassword=1234?MaxPlayers=50?ReservedPlayerSlots=25?INSERT?QueryPort=50002?Port=5002?SeamlessIP=192.168.1.225?RCONEnabled=true?RCONPort=28450 -log -server -NoBattlEye
exit
But what I am getting is this:
start /high ShooterGame\Binaries\Win64\ShooterGameServer.exe Ocean?ServerX=0?ServerY=0?AltSaveDirectoryName=0000?ServerAdminPassword=1234?MaxPlayers=50?ReservedPlayerSlots=25?INSERT?QueryPort=50002?Port=5002?SeamlessIP=192.168.1.225?RCONEnabled=true?RCONPort=28450 -log -server -NoBattlEye
exit???????INSERT?????
Which is the expected output, but with the unexpected re-addition of every symbol in the ECHO command which did not designate a variable at the end of the output (in this case ???????INSERT?????), just after the exit.
I'm stumped... I hope someone has an idea what I'm doing wrong here.
Okay, I applied the idea that aschipfl provided, and it seems to work now.
The IF NOT "%%b"=="" line seems to have done the trick, after I added the final line with the exit using another ECHO. My full script (including loop and write to file) is now like this:
#ECHO OFF
SETLOCAL EnableDelayedExpansion
SET "Insert=SettingOne=True?SettingTwo=False?SettingThree=1.000000"
FOR /l %%x IN (0, 1, 14) DO (
FOR /l %%y IN (0, 1, 14) DO (
IF %%x LSS 10 (SET XNum=0%%x) ELSE (SET XNum=%%x)
IF %%y LSS 10 (SET YNum=0%%y) ELSE (SET Ynum=%%y)
SET "FileNum=!XNum!!YNum!"
SET "FileName=Start !FileNum!.bat"
ECHO Filename: !FileName!
REN "!FileName!" temp.txt
(
FOR /F "tokens=1-12 delims=?" %%a IN (temp.txt) DO (
IF NOT "%%b"=="" (
ECHO %%a?%%b?%%c?%%d?%%e?%%f?%%g?%Insert%?%%h?%%i?%%j?%%k?%%l
ECHO exit
)
)
) >edited.txt
REN edited.txt "!FileName!"
DEL /q temp.txt
ECHO has been updated
)
)
This is now working exactly as intended.
It's quite possible that there is a more elegant way of doing this, and I am cartain that there is a way of making this more general and less hard-coded, but it's good enough for my purposes.
I thank you for your help!
Coding is not my speciality but I have come across a problem and google search has guided me to using batch file process in solving it. Essentially I have a couple of thousands of files that need to be moved into folders and they have a very simple file structure, listed below:
UK--London--Filename.pdf
UK--London--Filename2.pdf
UK--Manchester--Filename3.pdf
UK--Liverpool--Filename4.pdf
UK--Chester--Filename5.pdf
I would like the script to:
1. Pick up the first "--" check if the folder exists, if not create it
2. Pick up the second "--" check if the folder exists, if not create it
3. As there might be more than two "--", ignore the rest
4. Move file into the subfolder
To that end, the output should be some like (note "FILETEST" is the folder I am using to test the script):
C:\FILETEST\UK\London\UK--London--Filename.pdf
C:\FILETEST\UK\London\UK--London--Filename2.pdf
C:\FILETEST\UK\Manchester\UK--Manchester--Filename3.pdf
C:\FILETEST\UK\Liverpool\UK--Liverpool--Filename4.pdf
C:\FILETEST\UK\Chester\UK--Chester--Filename5.pdf
I have had an attempt to re-using a script from another question in stackoverflow (Batch create folders based on part of file name and move files into that folder). I understand that it would not do exactly what I need, but cant seem to get any output.
#ECHO OFF
SETLOCAL
SET "sourcedir=c:\FILETEST"
PUSHD %sourcedir%
FOR /f "tokens=1*delims=--" %%a IN (
'dir /b /a-d *.*.*'
) DO (
ECHO MD %%a
ECHO MOVE "%%a.%%b" --\%%a\
)
POPD
GOTO :EOF
Apologies for any headaches caused, I am hoping this is a simple one to solve.
Thank you,
Panos
#ECHO OFF
SETLOCAL
SET "sourcedir=c:\FILETEST"
PUSHD %sourcedir%
FOR /f "tokens=1,2*delims=-" %%a IN (
'dir /b /a-d *--*--*.*'
) DO if "%%c" neq "" (
ECHO MD "%%a"
ECHO MD "%%a\%%b"
ECHO MOVE "%%a--%%b--%%c" ".\%%a\%%b\"
)
POPD
GOTO :EOF
Read the directory list of files in the current directory, (/a-d = no directorynames) that match *--*--*. Tokenise so that %%a acquires the part before the first --sequence, %%b the second and %%c the remainder.
If %%c is not empty then make the directories ".\%%a" and ".\%%a\%%b" (quoted because any spaces in the name would otherwise be seen as "create two directories") then move the file, again quoted for the same reason.
Note that each character individually between delims= and the close-quote is a delimiter - a delimiter-string is not supported. Consequently, this code will pick up - as well as --- and any other sequence of - and try to process it. You could gate the create/move further by adding if exist "%%a--%%b--%%c" directly after the if "%%c" neq ""before the (.
The md will create a directory if the target name does not already exist, and produce an error-message if it already exists. To suppress the error message, append 2>nul to the md lines.
My task is to create a script to copy dated sub folders from within folders to another location. The base folders are named 16 hex characters, each has a folder for the year, inside a folder for the month, and inside that a folder for a date, which contains binary (dump) files I need to copy out. There are about 50 folders with around 5 days across two months that I care about (for now), so I want to capture something like the following:
C:\DUMPS\2401010153783FB6\2016\09\29*.*
Rather than doing a ton of copypaste script or manually trying to get all the folders I want (human error prone), I thought I should be able to create a BAT file to loop through an array of folder names and get at the files I care about. I found a very helpful piece in the answer from user DSS at the following:
How to loop through array in batch?
My problem is that his example shows how to use CALL ECHO instead of just ECHO, but I want to use the string value as part of a folder name, not just print to the console. Here's the work-in-progress code I've developed:
SET COPYPATH=C:\Dumps
SET DESTPATH=D:\NewDumps
SET FOLDERNAME[0]=2401010153783FB6
SET FOLDERNAME[1]=2401010753BBBBCE
SET "X=1" # array start from index 1
:Looping
IF DEFINED FOLDERNAME[%X%] (
CALL ECHO %%FOLDERNAME[%X%]%% REM Works great, I see the value of FOLDERNAME[0] on console.
SET TEMPVAR= CALL ECHO %%FOLDERNAME[%X%]%% REM No good! Result is CALL ECHO.
SET TEMPVAR= CALL %%FOLDERNAME[%X%]%% REM No good! Result is CALL X.
SET TEMPVAR= %FOLDERNAME[%X%]% REM No good! Result is literal FOLDERNAME[0]; I want the value of FOLDERNAME[0].
IF EXIST %TEMPVAR%\2016\09\28 (
COPY %TEMPVAR%\2016\09\28 %DESTPATH%\%FOLDERNAME[%X%]%%\2016\09\28
)
IF EXIST %COPYPATH%\%FOLDERNAME%\2016\09\29 (
COPY %COPYPATH%\%FOLDERNAME%\2016\09\29 %DESTPATH%\%FOLDERNAME%\2016\09\29
)
IF EXIST %COPYPATH%\%FOLDERNAME%\2016\09\30 (
COPY %COPYPATH%\%FOLDERNAME%\2016\09\30 %DESTPATH%\%FOLDERNAME%\2016\09\30
)
IF EXIST %COPYPATH%\%FOLDERNAME%\2016\10\01 (
COPY %COPYPATH%\%FOLDERNAME%\2016\10\01 %DESTPATH%\%FOLDERNAME%\2016\10\01
)
IF EXIST %COPYPATH%\%FOLDERNAME%\2016\10\02 (
COPY %COPYPATH%\%FOLDERNAME%\2016\10\02 %DESTPATH%\%FOLDERNAME%\2016\10\02
)
SET /A "X+=1"
GOTO :Looping
)
I would be fine with using a temporary variable for either path, or I could use the concatenated %COPYPATH%\%FOLDERNAME% syntax (which worked when I set this up for a one-off test, but now I want to loop it 50 times). Any advice is much appreciated!
how about...
#echo off
setlocal enabledelayedexpansion
SET "COPYPATH=C:\Dumps"
SET "DESTPATH=D:\NewDumps"
SET "FOLDERNAME[0]=2401010153783FB6"
SET "FOLDERNAME[1]=2401010753BBBBCE"
rem set start to first item index, stop to last
SET/a start=0, stop=1
SET "DAYS=2016\09\28,2016\09\30,2016\10\01"
for /L %%i in (%start%,1,%stop%) do (
echo !FOLDERNAME[%%i]!
set "TEMPVAR=%COPYPATH%\!FOLDERNAME[%%i]!"
set "DESTVAR=%DESTPATH%\!FOLDERNAME[%%i]!"
for %%d in (%DAYS%) do (
rem if it works, remove echo from line below
IF EXIST "!TEMPVAR!\%%d" echo COPY "!TEMPVAR!\%%d" "!DESTPATH!\%%d"
)
)
exit /B
how about
for /L %%a in (0,1,1) do (
for %%b in (2016) do (
for %%c in ("09 28" "09 29" "09 30" "10 01" "10 02") do (
call :sub %%a %%b %%~c
)
)
)
...whatever, etc...
goto :eof
:sub
call set "foldername=%%foldername[%1]%%"
IF EXIST %COPYPATH%\%FOLDERNAME%\%2\%3\%4 (
echo COPY %COPYPATH%\%FOLDERNAME%\%2\%3\%4 %DESTPATH%\%FOLDERNAME%\%2\%3\%4
)
goto :eof
where the echo is there to show you what is proposed, delete that echo keyword to execute.
Note that you offer no explanation of why you switch from tempname to copypath\foldername in your code, so I've assumed it's incomplete substitution.
I suppose all your:
SET TEMPVAR=....
were attempts to save the result of a command into a variable
Well I have to dissapoint you on that ... there is no possibility (yet) to assign directly the result of a command to a veriable in batch ( sorry ). Indirect ways do exist: for /f-loops, use of temporary file, ... but they could make your code more complex
The command you are looking for is
call set tempvar=%%foldername[%x%]%%
The call is used to execute the commands you gave him as parameter inside a batch-file.
So the command will look like (suppose x=0) set tempvar=%foldername[0]% and the call will just execute that (if x=0 off course...).
Good luck!
PS: consider #Magoo's answer to improve your batch script and #elzooilogico if you'd consider to use delayed epansion.
Firstly i am still a beginner at scripting so please be gentle.
I am creating a back up script and I want to be able to search network for part of a file name (which located on a networked External HDD for Backup use), then return the dir to use later to back up the contents of a USB device.
The folder name I am searching for consists of 4 elements as follows: Date-1234567-Type-User.
The part i am looking to search for is the 7 digit id number which is unique where as the other elements may be duplicated a number of times.
the current script i have been using is not returning anything it is giving file not found, could someone point me in the right direction my script is as follows, also i am working on windows 7.
#echo off
:start
set /p path=enter id no.:
for /r %%# in (.) do (
Echo %%~nx# | find "string" "%path%" 1>NUL && (
Echo Full Path: %%~nx#
Echo Filename : %%~nx#
Echo Directory: %%~p#
)
)
if defined p (
echo %p%
) else ( Echo file not found
)
pause
goto backup
:backup
if not exist "%p%"\"usb download" md "%p%"\"usb download"
if exist (
xcopy /y "USB" "%p%"\usb download
)
pause
goto start
the Xcopy lines work on their own creating a new folder and copying the contents however i have to write the complete file path manually which is quite extensive and laborious so if i could automate this it would help dramatically.
I thank you for your help in advance, I have googled and searched my heart out, but as I said I am a beginner and my brain is melting.
thanks all.
for /f "delims=" %A in ('dir /ad /b /s z:\*1234567*') do echo %A
This should find it.
This question already has an answer here:
Copying files based on date modified in batch file
(1 answer)
Closed 9 years ago.
Thanks for visiting. My question: I receive daily files that are stored as .csv's, typically with the following format: DEVICE_DEVICENumber_DateID. I'd like to move these files into their respective monthly folders (ie 201401) based on the Date portion of the DateID. The DateID is written as the date first followed by a unique device ID (ex YYYYMMDDUNIQUEID - 2014010110).
If I create folders like the one mentioned above (i.e. 201401, 201402, 201403, etc.), is there a simple way to pull a portion of the string to match the folder that I want to direct the file to?
Thanks for your help!
Calbruin
Typical filename:
GO.YO_WTR_SOO5_PT_20140102110.csv
#ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
SET "sourcedir=.\csvfiles"
SET "destdir=c:\destdir"
FOR /f "delims=" %%a IN ('dir /b /a-d "%sourcedir%\*.csv" ') DO (
SET "csvname=%%~na"
IF NOT "!csvname:*_=!"=="!csvname!" (
CALL :finddate
ECHO MD "%destdir%\!csvname!"
ECHO MOVE "%sourcedir%\%%a" "%destdir%\!csvname!\"
)
)
GOTO :EOF
:finddate
IF /i "%csvname:~-6%"=="_audit" SET "csvname=%csvname:~0,-6%"
IF "%csvname:*_=%"=="%csvname%" SET "csvname=%csvname:~0,6%"&GOTO :EOF
SET "csvname=%csvname:*_=%"
GOTO finddate
When applied to a directory '.\csvfiles` with content
GO.YO-WTR-SOO5-PT-20140102110.csv
GO.YO_WTR_SOO5_PT_20140102110.csv
Produced output
MD "c:\destdir\201401"
MOVE ".\csvfiles\GO.YO_WTR_SOO5_PT_20140102110.csv" "c:\destdir\201401\"
The required commands are merely ECHOed for testing purposes. After you've verified that the commands are correct, change ECHO MD to MD to actually create the directories. Append 2>nul to suppress error messages (eg. when the directory already exists)
Change ECHO MOVE to MOVE to actually move the files. Append >nul to suppress report messages (eg. 1 file moved)
Amendment - adjustment for ..._audit.csv.