I have a simple script that launches an XML file path in Notepad++
Unfortunately, the generated file path for some of the files is incorrect and I'm trying to compensate for it in the script (ideal fix is clearly to resolve the path issue but at the moment this is not an option).
Here is what I have, apologies for the code I'm very new to Batch Script...
set /p filePath= Enter file path:
if "%filePath%" == "1" goto xmlMenu
else if "%filePath%" == "file://path/of/file/*/*/*/*/A/*.XML"
set filePath="file://path/of/file/*/*/*/*/B/*.XML"
goto openXML
I would like the filePath variable to inherit the rest of the path from the user input but at the moment its explicitly setting the path with the wildcards. There also seems to be a problem with the way I have stated the condition as it appears to set the path to /B/*.XML regardless of the else if condition.
There are many errors, but as you say, youre new.
First, if the filepath you enter is "1" then you should arrive safely at xmlmenu.
The next line will generate an error because with an else clause,
the If-true instruction must be (parenthesised)
the ( must occur on the same physical line as the if
the sequence ) else ( must all be on the same physical line (which need not be the same as the if)
In any case, the else is redundant. If the condition is not true, the jump will not happen, hence the next statement line will be executed.
if...==... is true if the string on the left is identical to the sring on the right, hence you would need for the entered string to be absolutely identical to file://path/of/file/*/*/*/*/A/*.XML for the if to be true. The action-statment also must be on the same physical line as the if, so this is a second source of syntax errors.
the set is then executed, assigning that famous string (in quotes) to the variable filepath. And then we're off to openxml.
Note that \ is a path separator in Windows; / is a switch-specifier.
Now - if you were to explain what you want to do - perhaps enter a filename which may be somewhere in a directory structure and if it's found then assign what? to the variable filepath. One or two examples would be a good idea.
This should get a usable system going. The numeric check is incomplete, but would be adequate. I've not made it bullet-proof against a user determined to break it...
#ECHO OFF
SETLOCAL
:again
SET "filepath="
set /p filePath="Please enter filepath as yyyymmdd : "
IF NOT DEFINED filepath GOTO :EOF
if "%filePath%" == "1" goto xmlMenu
:: test to see whether entered data is numeric
SET /a "filepath=%filepath%" 2>nul
IF %filepath%==0 echo invalid entry - numerics only, please&GOTO again
IF %filepath% lss 19800100 GOTO badymd
IF %filepath% gtr 20991231 GOTO badymd
SET filepath=\path\prefix\%filepath:~0,4%\%filepath:~4,2%\%filepath:~6,2%
:: This is just to show the filepath constructed
ECHO filepath is "%filepath%"
IF NOT exist "%filepath%\A\*.xml" ECHO "%filepath%\A\*.xml" does NOT exist&GOTO again
IF NOT exist "%filepath%\B\*.xml" ECHO "%filepath%\B\*.xml" does NOT exist&GOTO again
SET "filepath=%filepath%\B\*.xml"
goto openXML
:badymd
ECHO invalid entry - 4 digits FOR year, 2 FOR month an 2 FOR day, please
GOTO again
:xmlmenu
ECHO AT xmlmenu&GOTO :eof
:openxml
ECHO AT openxml with filepath="%filepath%"&GOTO :eof
GOTO :EOF
Obviously, change the path prefix to suit.
For an input of 20140220, the path ....\2014\02\20 would be constructed. Then there's a check for and this +\B\*.xml - I can only presume both must exist to proceed to openxml.
Related
SET /P A=SPR
IF /I "%A:~,1%" EQU "d" (
IF EXIST %1 (
IF NOT EXIST %2 (
COPY %1 %2
)
)
)
First, I suggest to open a command prompt window and run the following commands:
set /?
if /?
copy /?
call /? ... explains %1 and %2.
For each command the help is output which you should read from top to bottom.
This small batch file first prompts the batch file user for a string with prompt text SPR. The string entered by the user is assigned to environment variable A if the user enters anything at all as expected by this batch file.
Next a case-insensitive string comparison is done to check if the first character of the string entered by the user is d or D.
It would be much better to use comparison operator == instead of EQU in this case. == always makes a string comparison while EQU first tries to compare integers and if that fails because it is not possible to convert both comparison arguments to signed 32-bit integers, a string comparison is done. The second argument d is not a signed 32-bit integer.
The batch file must be started with two arguments being in this case two file names without or with wildcards. I suppose the batch file expects two file names without or with path without wildcards.
If the first condition is true, the batch file checks next if source file with name passed to batch file as first argument exists and next if target file passed to batch file as second argument does not exist. The source file is copied to target file if those 2 conditions are true.
More fail safe would be:
SET "Input=?"
SET /P "Input=SPR: "
IF /I "%Input:~0,1%" == "d" (
IF "%~1" == "" GOTO :EOF
IF "%~2" == "" GOTO :EOF
IF EXIST "%~1" IF NOT EXIST "%~2" COPY "%~1" "%~2"
)
The environment variable Input is now predefined with ? as string. So when the user just hits RETURN or ENTER without entering anything at all, the environment variable Input is nevertheless defined with ? as string and next command IF works, but of course the condition is in this case false.
The IF condition as is results in an exit of batch execution because of an syntax error only when the first entered character is ". In all other cases the IF condition works now and runs a case-insensitive string comparison.
The improved batch file checks next if the batch file was really started with two argument strings as expected and exit the batch file with a jump to predefined label for End Of File if either first or second argument is an empty string (or a string consisting only of one or two double quotes).
I've been working on creating a batch file which moves files from one dir to another dir and if the filename already exists rename it then move it over.
I'm really new to creating batch files so heres what I have so far
set temp=C:\Users\Daniel\Desktop\a\a1
set dir=C:\Users\Daniel\Desktop\a\
set /a "counter=0"
set "duplicate=-copy^("
set "bracket=^)"
if exist "%temp%" ( ^
for %%i in (%temp%\*) ^
do ^
if exist "%dir%\%%~ni%%~xi" ( call :checkFileName %%~ni %%~xi) ^
ELSE ( move %temp%\%%~ni%%~xi %dir% ) )^
ELSE ( echo doesnt exist)
:checkFileName
echo test
set fileName=%1
set fileExtenstion=%2
set /a "counter+=1
rem Do whatever you want here over the files of this subdir, for example:
if exist %dir%%fileName%%duplicate%%counter%%bracket%%fileExtenstion% ( IF defined %1 (
IF defined %2 (call :checkFileName %1 %2 )) ) ELSE (ren %temp%\%fileName%%fileExtenstion% %fileName%%duplicate%%counter%%bracket%%fileExtenstion% )
timeout 30
goto :eof
:increment
set /a "counter+=1"
goto :eof
I've no idea to increment a var before calling my checkFileName function. I think recursively calling the same function is the right idea but I'm a bit rusty with the commands/syntax as I only started this on friday.
Any advice or pointers would be appreciated. (If you know any useful links/books that are worth a look let me know!)
timeout 600
#ECHO OFF
SETLOCAL
set "tempdir=C:\Users\Daniel\Desktop\a\a1"
set "dir=C:\Users\Daniel\Desktop\a"
set "tempdir=U:\sourcedir\t w o"
set "dir=U:\destdir"
set "duplicate=-copy("
set "bracket=)"
if exist "%tempdir%" (
for %%i in ("%tempdir%\*") do (
if exist "%dir%\%%~nxi" ( call :checkFileName "%%~ni" "%%~xi"
) ELSE (
move "%tempdir%\%%~nxi" "%dir%" >nul
)
)
) ELSE (
echo doesnt EXIST
)
GOTO :eof
:checkFileName
set "fileName=%~1"
set "fileExtenstion=%~2"
set /a counter=0
:nexttry
set /a counter+=1
rem Do whatever you want here over the files of this subdir, for example:
if exist "%dir%\%fileName%%duplicate%%counter%%bracket%%fileExtenstion%" GOTO nexttry
move "%tempdir%\%fileName%%fileExtenstion%" "%dir%\%fileName%%duplicate%%counter%%bracket%%fileExtenstion%" >nul
goto :eof
Here's a revised version. I'll explain the changes I've made:
#echo off turns off command-echoing
setlocal ensures any changes made to the environment are backed-out when the procedure ends.
I've added to extra sets to re-set the directories to suit my system. You'd need to delete these two lines for yours.
temp is a special name which points to a temporary directory. One of quite a few. Best not to use that particular name - replaced with tempdir
set when used for a numeric set doesn't require quotes. In a string-set, the syntax set "var=value" is used to ensure that trailing spaces on the command-line are not included into the value assigned (which can cause chaos - spaces are sort of - invisible.) Note that in a string set, spaces on both sides of the = are significant...
I prefer to assign directorynames into variables without the trailing \. This allows the value to be extended with the least gymnastics. Personal preference - but you used it both ways...
The carets are not required before ( and are only required before ) where the syntax would close an open parenthesis (ie. in a parenthesised statement-sequence as may occur in an if, else or do.) Used arbitrarily, this can lead to stray literal carets in filenames, for instance.
carets at end-of-line is a valid but easily-lost and a little-used technique. The rule for breaking statements over multiple lines is crudely, keep do, if or else on the same physical line as its ( and else on the same physical line as the closing-parenthesis that precedes it. Then no eol-caret is required.
Batch simply charges on through statements. It has no concept of the end of a procedure and needs to be told when the procedure ends. This can be done with a goto :eof statement (which jumps to the physical end-of-file) or an exit /b statement (which returns from a subroutine, optionally setting errorlevel. goto :eof effectively does the smae thing in most circumstances and is way more common.)
%%~nxi means the name-and-extension of the file %%i. Of course, it's quite legal to use %%~ni and its counterpart individually, but it's not necessary. Note however that these parts should be despatched in "quotes" to the subroutine because each part may contain spaces. "quotes"make a spaces-containing-string appear as one string with spaces rather than a series of strings.
>nul redirects the move command's report "1 file(s) moved" to the bit-bucket.
Setting the two variables within checkfilename should be done after removing the quotes applied in the call - that's the purpose of the ~ before the parameter-number.
counter can be set to zero, then incremented.
If the proposed new filename exists, then simply increment the number and try again until you hit a name that doesn't exist. Yes - counter will run out eventually. It tops out at 2**31-1. Might take a while...
Note the use of quotes in the if exist and move. This is to guard against spaces in file/directorynames. The same goes for the for %%i in ("%tempdir%\*") used earlier...you may notice that in my testing, I used (deliberately) a directoryname that contained spaces. As it happens, the filenames I used also had spaces in them.
One last warning - There is no doubt that some odd filenames may choke on these procedures, but they should be few and far between. Filenames containing carets may be a problem, for instance.
Welcome to batch!
Unless this is a learning project, I recommend you study the XCOPY command.
My colleague and I have been pulling our hair out all day over this.
We have a simple Windows batch file. We want it to read from a text file whose file path we are generating programmatically, take the single numeric value in this file, and compare it to a local variable. But we're getting completely inexplicable behavior.
The file contains a single scalar number, such as the number 2. Here's the code:
ThisAppFlagFileName=foo.txt
if not exist "%HOMEPATH%\ourcompanyname\%ThisAppFlagFileName%" (
ECHO do something here
) else (
SET /P InstalledVersion=<"%HOMEPATH%\ourcompanyname\%ThisAppFlagFileName%"
ECHO We think the file contains: %InstalledVersion%
IF %InstalledVersion% GEQ %ThisVersionInstallDataNum% (
ECHO Version %ThisVersion% of the %ThisAppVisibleName% has already been installed for this user; exiting.
GOTO TheEnd
)
)
:TheEnd
Echo END
We keep getting an error reading 2 was unexpected at this time. So we inserted some trace message and, just in case the else was problematic, stuck to two different if statements:
ThisAppFlagFileName=foo.txt
if not exist "%HOMEPATH%\ourcompanyname\%ThisAppFlagFileName%" (
ECHO do something here
)
ECHO Trace Message 1 before IF
if exist "%HOMEPATH%\ourcompanyname\%ThisAppFlagFileName%" (
ECHO Trace Message 2 after IF before CD
SET /P InstalledVersion=<%HOMEPATH%\ourcompanyname\%ThisAppFlagFileName%"
ECHO We think the file contains: %InstalledVersion%
IF %InstalledVersion2% GEQ %ThisVersionInstallDataNum% (
ECHO Version %ThisVersion% of the %ThisAppVisibleName% has already been installed for this user; exiting.
GOTO TheEnd
)
)
:TheEnd
Echo END
And we see only the first trace message (before the if statement), and not the second trace message. So our conclusion is that somehow the content of the file is being interpolated into the line if exist "%HOMEPATH%\ourcompanyname\%ThisAppFlagFileName%", but of course we don't understand why the first if not exist works but the second doesn't.
Can anyone spot the mistake, please? Environment is Windows 7 cmd.exe window, but we are hoping to deploy to both Windows 7 and Windows XP.
The issue here is that the entire IF expression is evaluated before the SET /P statement within it can be executed. InstalledVersion is not set yet, and so this invalid expression is evaluated:
IF GEQ 2 (
Nothing inside of the IF expression executes because it cannot be completely evaluated.
A solution is to enable delayed expansion and replace %InstalledVersion% with !InstalledVersion!, as described in this post.
You can also restructure the code so the GEQ comparison happens after the IF expression.
Your code have several errors. The first line:
ThisAppFlagFileName=foo.txt
missed a set command, so it is tryed to be executed as ThisAppFlagFileName command. This mean that ThisAppFlagFileName variable is NOT defined in your program, so InstalledVersion variable is never read from the file.
All references to InstalledVersion variable must use Delayed Expansion, that is, enclose they between exclamation marks instead percents and include setlocal EnableDelayedExpansion command at beginning of your program.
setlocal EnableDelayedExpansion
set ThisAppFlagFileName=foo.txt
if not exist "%HOMEPATH%\ourcompanyname\%ThisAppFlagFileName%" (
ECHO do something here
) else (
SET /P InstalledVersion=<"%HOMEPATH%\ourcompanyname\%ThisAppFlagFileName%"
ECHO We think the file contains: !InstalledVersion!
IF !InstalledVersion! GEQ %ThisVersionInstallDataNum% (
ECHO Version %ThisVersion% of the %ThisAppVisibleName% has already been installed for this user; exiting.
GOTO TheEnd
)
)
:TheEnd
Echo END
You must be aware that all variables that are modified inside parentheses must also be enclosed in exclamation marks instead percent signs. Search for "delayed expansion" for details.
I want to use a Windows batch file in to copy a file (myfile0001.bdg) from one specific directory to another. But I want to check if the file in the target directory exists and if the answer is yes, increment the file with 0001 and check again if the file exists (myfile0002.bdg) and so on, until the file does not exist, and copy the file with the new title.
So, if in the target directory, I have these files:
myfile0001.bdg
myfile0002.bdg
myfile0003.bdg
myfile0004.bdg
myfile0005.bdg
myfile0006.bdg
The new file should be named myfile0007.bdg. The next time I will execute the batch, the new file will be myfile0008.bdg, etc.
I know there is a command "IF EXIST" but I don't know to do what I need.
==============
I'm under Windows 7 x32
The source directory is "C:\USERS\RAMBYTES\DOCUMENTS\"
The target directory is "P:\BACKUP\"
The file is "MYFILE0001.BDG"
Something like this:
#echo off
set source_file=C:\USERS\RAMBYTES\DOCUMENTS\MYFILE0001.BDG
set target_dir=P:\BACKUP\
set done=0
for /l %%i in (1,1,1000) do (
call :check_and_copy %%i
if errorlevel 1 goto :eof
)
goto :eof
:check_and_copy
setlocal EnableDelayedExpansion
set num=000000%1
set fnum=!num:~-4!
set fname=%target_dir%\myfile%fnum%.bdg
rem echo %fname%
if not exist "%fname%" (
echo copying %source_file% to %fname%
exit /b 1
)
exit /b 0
There is no error handling in case there are more than a 1000 files present in the target directory. If you want to increas the file limit, you need to adjust the "main" for loop and the "formatting" of the number in the sub-program
The trick with adding the leading zeros was taken from here: https://stackoverflow.com/a/9430912/330315
#ECHO OFF
SET destdir=c:\destdir
SET newname=myfile0000
FOR /f %%i IN (' dir /b /on %destdir%\myfile????.bdg ' ) DO SET newname=%%~ni
SET newname=1%newname:~-4%
SET /a newname+=1
SET newname=myfile%newname:~-4%.bdg
COPY myfile0001.bdg %destdir%\%newname%
change the destination directory as desired, and include the source directory if required.
Take the file name.
Extract the numeric part.
Check if the corresponding target name exists.
If so,
4.1) increase the numeric part;
4.2) if it doesn't exceed the highest possible number go to Step 3;
4.3) otherwise terminate.
If the target name doesn't exist, copy the file with the current numeric part and terminate.
Although algorithmically the condition in 4.2 may be more natural to be checked just after increasing the numeric part, like I put it above, the below script performs the check at a different point, at the beginning of the loop, which starts just after extracting the original numeric value from the source filename. Implementationally, that seemed to me more convenient.
In all other respects, the script implements the same algorithm:
#ECHO OFF
SET "fname=%~n1"
SET counter=1%fname:~-4%
:loop
IF %counter% GTR 19999 (
1>&2 ECHO Cannot copy the file: no free slots.
EXIT /B 1
)
SET "targetname=%~2\%fname:~0,-4%%counter:~1%%~x1"
IF EXIST "%targetname%" (
SET /A counter+=1
GOTO loop
) ELSE (
COPY %1 "%targetname%"
)
To explain some parts:
The tilde (~) in references to positional parameters means de-quoting of the correspondent parameter.
Sometimes in the script, the tilde is also directly followed by a modifier. Two modifiers are used here, n and x. The former causes the parameter to expand to the corresponding file name only (without the path and the extension) and the latter extract only the extension.
You can learn more about modifier in the built-in of the FOR command (by running
The fname environment variable is needed because extracting of name parts can only be done on environment variables. The %fname:~-4 expression, in particular, evaluates to the last four characters of the fname value. More specifically, it reads: extract the substring that starts at the 4th character from the end and, as -4 isn't followed by another argument, includes all the characters from that point till the end of the string.
Another similar-looking expression, %fname:~0,-4%, does the opposite: it returns the contents of fname except the last four characters. The meaning of the numbers is this: extract the substring that starts at the beginning of the string (offset 0) and spans the range up to and including the character at the offset of 4 from the end.
One more expression of this kind, %counter:~1, extracts the characters starting from the second one (i.e. offset 1) and up to the end of string (no second argument).
Run SET /? to find out more about string expressions.
The counter implementation may also require explanation. The 1 added in front of the numeric part of the file name is needed so that the entire value could be interpreted and processed correctly when incrementing it.
The thing is, a numeric value starting with a 0 is treated as an octal by the CMD command processor, so, putting 1 at the beginning makes it to interpret the number as a decimal, which it actually is. When constructing the complete name of the target file, we simply need to discard the added 1, which is what the %counter:~1 is used for.
I am new here and my english is not very good so first, pardon me.
My problem is that this batch keep reading the strings inside the variable "IF EXIST %FSIZE%"; i meant, in case that variable does not exist, the batch keep reading inside the () brackets instead go on the rest of the stings.
If %FSIZE% exist, the batch perform the 2 task i assign: (1.) If size is equal, goto cifiles5x. (2.) If size is NOT equal it uses 7z to extract the file i want to be there.
If %FSIZE% DOES NOT EXIST, the batch keep saying "765952 was not expect at this moment".
I am taking the advices on ss64.com like don't use brackets or quotes when comparing numeric values (%size% EQU 765952) but i don't understand why it does not continue to where the ) ends.
I have also try to link the commands with "&&" so i can erase the brackets but the results are the same.
I know there's 2 spaced patches without quotes; they are unquoted because if i did the size checker won't work.
Thanks for reading this.
EDIT: Batch modified according to suggestions made.
#ECHO OFF
TITLE Log checker
COLOR 0F
SET FSIZE=%ProgramFiles(x86)%\Ces\Log Files Ver\LogVbReg_r2.dll
SET InsDIR=%ProgramFiles(x86)%\Ces\Log Files Ver\
REM I didn't add "" on FSIZE and InsDIR because if i did, quote the variable will
REM result a doubled quoted patch and won't work.
CLS
ECHO ==============================================================================
ECHO = Log checker =
ECHO ==============================================================================
ECHO Checking if exist:
ECHO "%FSIZE%"
IF EXIST "%FSIZE%" (
ECHO It does exist, checking size...
FOR %%A IN ("%FSIZE%") DO SET SIZE=%%~ZA
IF "%SIZE%" EQU "765952"
ECHO Size is right, jumping to CIFILES5
GOTO CIFILES5x
) ELSE (
ECHO Size is not right, extracting the needed file...
7z.exe e "Data_X.*" -o"%InsDIR%" -y "LogVbReg_r2.dll"
GOTO CIFILES5x)
ECHO Does not exist; extracting file...
REN Data_X.* Data_X.exe
Data_X.exe
TIMEOUT 2>NUL
REN DATA_X.* Data_X.dat
:CIFILES5x
ECHO Reach cifiles5x
PAUSE
IF EXIST "%TEMP%\9513.CES" (GOTO OPS) ELSE (GOTO LNKD)
You have space in your file name of %FSIZE%. As result text after space it is treated as command for if. Try using quotes around "%FSIZE%" or do CD to folder first and than check for just file name.
You have several minor errors:
If the file name may have spaces, it MUST be enclosed in quotes in all cases:
IF EXIST "%FSIZE%" (
FOR /F is used to read file CONTENTS. If you want to process file NAME, don't use /F option, but plain FOR command:
FOR %%A IN ("%FSIZE%") DO SET SIZE=%%~ZA
The ELSE part of an IF command must appear in a line alone:
GOTO CIFILES5x
) ELSE (
The same thing for a parentheses:
GOTO CIFILES5x
)
Excepting if the opening parentheses appear in the same line. This is correct:
IF EXIST "%TEMP%\9513.CES" (GOTO OPS) ELSE (GOTO LNKD)
If you compare numbers for equality (EQU, NEQ) then don't matter if they are enclosed in quotes or not. Just in case of GTR, GEQ, LSS and LEQ they must have NOT quotes. However, in your case, this is not a problem...