I have a SQL loader command which I am calling from a batch script. The data filename is not constant. We have a timestamp appended to it each time the file is generated and populated. So my batch file gets this data file in a variable Fname.
SET Fname=dir C:\Temp\TEST_*.dat /b
Now, when the Sqlldr commmand runs from the batch file
sqlldr USERID=xyz/xyz#db CONTROL='C:\Temp\TEST.ctl' LOG='C:\Temp\TEST.log' DATA= %Fname%
I get the error
LRM-00112: multiple values not allowed for parameter 'data'
I cannot enclose the variable Fname in single quotes. That does not work.
I checked How can I use a variable in batch script at sqlldr?
If I use the method specified in the discussion above and include it in the ctl file as infile %Fname% I still get the error as the variable Fname appears as dir C:\Temp\TEST_*.dat /b and I get error saying file not found.
How do I resolve this?
The simple solution below executes the command always with first TEST_*.dat file found in folder C:\Temp.
#echo off
for %%F in ("C:\Temp\TEST_*.dat") do (
sqlldr USERID=xyz/xyz#db CONTROL='C:\Temp\TEST.ctl' LOG='C:\Temp\TEST.log' "DATA=%%F"
goto AfterLoop
)
:AfterLoop
A perhaps better solution is executing the command always with newest TEST_*.dat file found in folder C:\Temp according to last modification date.
#echo off
for /F "delims=" %%F in ('dir "C:\Temp\TEST_*.dat" /B /O-D /TW 2^>nul') do (
sqlldr USERID=xyz/xyz#db CONTROL='C:\Temp\TEST.ctl' LOG='C:\Temp\TEST.log' "DATA=%%F"
goto AfterLoop
)
:AfterLoop
It would be possible to assign the name of the found file to an environment variable inside the FOR loop and run the command below AfterLoop. But this requires additional code to check if at least 1 file was found in the folder.
Those batch code snippets were developed using the help information output by running in a command prompt window for /? and dir /?.
2^>nul is redirecting the error output of command dir to device NUL if no file is found in folder to suppress the unwanted error message for this special use case.
My last hint:
In batch files always specify applications to run with full path and file extension (in double quotes if space in path/file name) to avoid being dependent on directories in environment variable PATH, except this is not possible as storage location of application executable is not known on batch execution.
I used the for loop code
#echo off
for %%F in ("C:\Temp\TEST_*.dat") do (
sqlldr USERID=xyz/xyz#db CONTROL='C:\Temp\TEST.ctl' LOG='C:\Temp\TEST.log' "DATA=%%F"
goto AfterLoop
)
:AfterLoop
and this worked.
I do not have the problem of multiple files as after I load it everytime, I move the file to a different folder. So at any given point I expect only one file.
Related
I have a simple copy from-to script for one of my friends who is missing a file 20 km from my desk.
When testing the script out I am prompted if my file shapes.atc is a file or a folder.
I can tell you that its a file. How can I automatically copy it with my friend needs to just double click the batch to get the file copying job done.
xcopy /s/y J:\"My Name"\"FILES IN TRANSIT"\JOHN20101126\"Missing file"\Shapes.atc C:\"Documents and Settings"\"His name"\"Application Data"\Autodesk\"AutoCAD 2010"\"R18.0"\enu\Support\Shapes.atc
A seemingly undocumented trick is to put a * at the end of the destination - then xcopy will copy as a file, like so
xcopy c:\source\file.txt c:\destination\newfile.txt*
The echo f | xcopy ... trick does not work on localized versions of Windows, where the prompt is different.
Actually xcopy does not ask you if the original file exists, but if you want to put it in a new folder named Shapes.atc, or in the folder Support (which is what you want.
To prevent xcopy from asking this, just tell him the destination folder, so there's no ambiguity:
xcopy /s/y "J:\Old path\Shapes.atc" "C:\Documents and Settings\his name\Support"
If you want to change the filename in destination just use copy (which is more adapted than xcopy when copying files):
copy /y "J:\Old path\Shapes.atc" "C:\Documents and Settings\his name\Support\Shapes-new.atc
The real trick is: Use a Backslash at the end of the target path where to copy the file. The /Y is for overwriting existing files, if you want no warnings.
Example:
xcopy /Y "C:\file\from\here.txt" "C:\file\to\here\"
echo f | xcopy /s/y J:\"My Name"\"FILES IN TRANSIT"\JOHN20101126\"Missing file"\Shapes.atc C:\"Documents and Settings"\"His name"\"Application Data"\Autodesk\"AutoCAD 2010"\"R18.0"\enu\Support\Shapes.atc
Referencing XCopy Force File
For forcing files, we could use pipeline "echo F |":
C:\Trash>xcopy 23.txt 24.txt
Does 24.txt specify a file name
or directory name on the target
(F = file, D = directory)?
C:\Trash>echo F | xcopy 23.txt 24.txt
Does 24.txt specify a file name
or directory name on the target
(F = file, D = directory)? F
C:23.txt
1 File(s) copied
For forcing a folder, we could use /i parameter for xcopy or using a backslash() at the end of the destination folder.
The /i switch might be what your after.
From xcopy /?
/I If destination does not exist and copying more than one file,
assumes that destination must be a directory.
Well, for the task as asked by just me the perhaps best solution would be the following command according to the incomplete advice of Andy Morris:
xcopy "J:\My Name\FILES IN TRANSIT\JOHN20101126\Missing file\Shapes.atc" "C:\Documents and Settings\His name\Application Data\Autodesk\AutoCAD 2010\R18.0\enu\Support\" /Q /R /S /Y
This works for this simple file copying task because of
specifying just the destination directory instead of destination file and
ending destination directory with a backslash which is very important as otherwise XCOPY would even with /I prompt for file or directory on copying just a single file.
The other parameters not related to the question are:
/Q ... quiet
/Y ... yes (OS language independent) on overwrite existing file
/R ... overwrite also read-only, hidden and system file
/S ... from specified directory and all subdirectories.
Well, I don't know if /S is really needed here because it is unclear if just J:\My Name\FILES IN TRANSIT\JOHN20101126\Missing file\Shapes.atc should be copied or all Shapes.atc found anywhere in directory tree of J:\My Name\FILES IN TRANSIT\JOHN20101126\Missing file.
The explanation for the parameters can be read by opening a command prompt window and running from within this window xcopy /? to get output the help for XCOPY.
But none of the provided solutions worked for a file copying task on which a single file should be copied into same directory as source file, but with a different file name because of current date and time is inserted in file name before file extension.
The source file can have hidden or system attribute set which excludes the usage of COPY command.
The batch file for creating the time stamped file should work also on Windows XP which excludes ROBOCOPY because by default not available on Windows XP.
The batch file should work with any file including non typical files like .gitconfig or .htaccess which are files without a file extension starting with a point to hide them on *nix systems. Windows command processor interprets such files as files with no file name and having just a file extension because of the rule that everything after last point is the extension of the file and everything before last point is the file name.
For a complete task description and the final, fully commented solution see the post Create a backup copy of files in UltraEdit forum.
Patrick's, Interociter Operator's and CharlesB's solutions do not work because using /Y does not avoid the file or directory prompt if the destination file does not already exist.
Andy Morris' and grenix's solutions can't be used for the single file copying task as destination must be the name of destination file and not the name of destination directory. The destination directory is the same as the source directory, but name of destination file is different to name of source file.
DosMan's and Govert's solutions simply don't work for files starting with a point and not having a file extension.
For example the command
xcopy C:\Temp\.gitconfig C:\Temp\.gitconfig_2016-03-07_15-30-00* /C /H /K /Q /R /V /Y
results in following error message on execution:
English: Could not expand second file name so as to match first.
German: Zweiter Dateiname konnte nicht so erweitert werden, dass er zum ersten passt.
And finally Denis Ivin's solution has the restriction that the operating system language dependent character for an automatic answering of the file OR directory prompt must be known.
So I thought about methods to get F for File on English Windows or D for Datei on German Windows or ? for ... on ... Windows automatically.
And it is indeed possible to determine the language dependent character for an automatic answering of the prompt.
A hack is used to get the language dependent letter from prompt text without really copying any file.
Command XCOPY is used to start copying the batch file itself to folder for temporary files with file extension being TMP for destination file. This results in a prompt by XCOPY if there is not already a file with that name in temporary files folder which is very unlikely.
The handler of device NUL is used as an input handler for XCOPY resulting in breaking the copying process after the prompt was output by XCOPY two times.
This output is processed in a FOR loop which is exited on first line starting with an opening parenthesis. This is the line on which second character defines the letter to use for specifying that destination is a file.
Here is a batch file using XCOPY with the code to determine the required letter for an automatic answering of the file or directory prompt to create a time stamped copy of a single file in same directory as the source file even if source file is a hidden or system file and even if the source file starts with a point and does not have a file extension.
#echo off
setlocal EnableExtensions DisableDelayedExpansion
rem Batch file must be started or called with name of a single file.
if "%~1" == "" exit /B
for /F "delims=*?" %%I in ("#%~1#") do if not "%%I" == "#%~1#" exit /B
if not exist "%~1" exit /B
if exist "%~1\" exit /B
rem Determine the character needed for answering prompt of
rem XCOPY for destination being a file and not a directory.
del /F "%TEMP%\%~n0.tmp" 2>nul
for /F %%I in ('%SystemRoot%\System32\xcopy.exe "%~f0" "%TEMP%\%~n0.tmp" ^<nul') do (
set "PromptAnswer=%%I"
setlocal EnableDelayedExpansion
if "!PromptAnswer:~0,1!" == "(" set "PromptAnswer=!PromptAnswer:~1,1!" & goto CopyFile
endlocal
)
echo ERROR: Failed to determine letter for answering prompt of XCOPY.
exit /B
:CopyFile
endlocal & set "PromptAnswer=%PromptAnswer%"
rem This is a workaround for files starting with a point and having no
rem file extension like many hidden files on *nix copied to Windows.
if "%~n1" == "" (
set "FileNameWithPath=%~dpx1"
set "FileExtension="
) else (
set "FileNameWithPath=%~dpn1"
set "FileExtension=%~x1"
)
rem Get local date and time in region and language independent format YYYYMMDDHHmmss
rem and reformat the local date and time to format YYYY-MM-DD_HH-mm-ss.
for /F "tokens=2 delims==." %%I in ('%SystemRoot%\System32\wbem\wmic.exe OS get LocalDateTime /format:value') do set "LocalDateTime=%%I"
set "LocalDateTime=%LocalDateTime:~0,4%-%LocalDateTime:~4,2%-%LocalDateTime:~6,2%_%LocalDateTime:~8,2%-%LocalDateTime:~10,2%-%LocalDateTime:~12,2%"
rem Do the copy with showing what is copied and with printing success or
rem an error message if copying fails for example on sharing violation.
echo Copy "%~f1" to "%FileNameWithPath%_%LocalDateTime%%FileExtension%"
for /F %%I in ('echo %PromptAnswer% ^| %SystemRoot%\System32\xcopy.exe "%~f1" "%FileNameWithPath%_%LocalDateTime%%FileExtension%" /C /H /K /Q /R /V /Y') do set "FilesCopied=%%I"
if "%FilesCopied%" == "1" (echo Success) else echo ERROR: Copying failed, see error message above.
This batch code was tested on German Windows XP SP3 x86 and English Windows 7 SP1 x64.
See the post Create a backup copy of files in UltraEdit forum for a similar, fully commented batch file explaining all parts of the batch code.
For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.
del /?
echo /?
exit /?
for /?
goto /?
if /?
rem /?
set /?
setlocal /?
wmic OS get /?
xcopy /?
Further the Microsoft article about Using command redirection operators should be read, too.
The trick of appending "*" can be made to work when the new extension is shorter. You need to pad the new extension with blanks, which can only be done by enclosing the destination file name in quotes. For example:
xcopy foo.shtml "foo.html *"
This will copy and rename without prompting.
"That's not a bug, it's a feature!" (I once saw a VW Beetle in the Microsoft parking lot with the vanity plate "FEATURE".) These semantics for rename go all the way back to when I wrote DOS v.1. Characters in the new name are substituted one by one for characters in the old name, unless a wildcard character (? or *) is present in the new name. Without adding the blank(s) to the new name, remaining characters are copied from the old name.
xcopy /s/y J:\"My Name"\"FILES IN TRANSIT"\JOHN20101126\"Missing file"\Shapes.atc C:\"Documents and Settings"\"His name"\"Application Data"\Autodesk\"AutoCAD 2010"\"R18.0"\enu\Support\*.*"
..should do it.
Good idea to do an:
IF NOT EXIST "C:\Documents and Settings\His name\Application Data\Autodesk\AutoCAD 2010\R18.0\enu\Support\Shapes.atc" ECHO/ && ECHO/ && ECHO * * * * * COPY FAILED - Call JustME at 555-555-1212 && ECHO/ && pause
(assuming you've done a rename of previous version to .old)
XCOPY /Z <----- restartable mode - good for large files.
The virtual parent trick
Assuming you have your source and destination file in
%SRC_FILENAME% and %DST_FILENAME%
you could use a 2 step method:
#REM on my win 7 system mkdir creates all parent directories also
mkdir "%DST_FILENAME%\.."
xcopy "%SRC_FILENAME% "%DST_FILENAME%\.."
this would be resolved to e.g
mkdir "c:\destination\b\c\file.txt\.."
#REM The special trick here is that mkdir can create the parent
#REM directory of a "virtual" directory (c:\destination\b\c\file.txt\) that
#REM doesn't even need to exist.
#REM So the directory "c:\destination\b\c" is created here.
#REM mkdir "c:\destination\b\c\dummystring\.." would have the same effect
xcopy "c:\source\b\c\file.txt" "c:\destination\b\c\file.txt\.."
#REM xcopy computes the real location of "c:\destination\b\c\file.txt\.."
#REM which is the now existing directory "c:\destination\b\c"
#REM (the parent directory of the "virtual" directory c:\destination\b\c\file.txt\).
I came to the idea when I stumbled over some really wild ../..-constructs in the command lines generated from a build process.
I had exactly the same problem, where is wanted to copy a file into an external hard drive for backup purposes.
If I wanted to copy a complete folder, then COPY was quite happy to create the destination folder and populate it with all the files.
However, I wanted to copy a file once a day and add today's date to the file.
COPY was happy to copy the file and rename it in the new format, but only as long as the destination folder already existed.
my copy command looked like this:
COPY C:\SRCFOLDER\MYFILE.doc D:\DESTFOLDER\MYFILE_YYYYMMDD.doc
Like you, I looked around for alternative switches or other copy type commands, but nothing really worked like I wanted it to.
Then I thought about splitting out the two different requirements by simply adding a make directory ( MD or MKDIR ) command before the copy command.
So now i have
MKDIR D:\DESTFOLDER
COPY C:\SRCFOLDER\MYFILE.doc D:\DESTFOLDER\MYFILE_YYYYMMDD.doc
If the destination folder does NOT exist, then it creates it.
If the destination folder DOES exist, then it generates an error message.. BUT, this does not stop the batch file from continuing on to the copy command.
The error message says:
A subdirectory or file D:\DESTFOLDER already exists
As i said, the error message doesn't stop the batch file working and it is a really simple fix to the problem.
Hope that this helps.
My Code
I have some straight forward code below that:
Checks if a file exists in my directory
Runs a for loop to get the first filename
Does stuff based on filename
Deletes the file
Checks if any other files exist in directory (if yes, repeat, if not, move on)
:MYLOOP
IF NOT EXIST "%mypath%\*.*" GOTO nofile
FOR %%F IN ("%mypath%\*.*") DO (
set filenameWithExt=%%~nxF
set filename=%%~nF
set filepath=%%~pF
)
do other filename specific tasks
del "%mypath%\%filenameWithExt%"
IF NOT EXIST "%mypath%\*.*" GOTO nofile
GOTO MYLOOP
:nofile
My Issue
I've used this code repeatedly and its worked like a charm, but on my most recent use it looks like its finding a 'ghost' file. When there are no FILES (there is a single archive FOLDER) in the directory, the if not exist check from step 1 above somehow is still passing. As a result, the set code in the for loop results in:
The system cannot find the file specified.
And it then appears as though it tries to delete my directory, saying:
\\mypath*, Are you sure (Y/N)?
I then have to manually terminate an otherwise automated batch.
My Question
Why is it passing the if not exist check, rather than skipping to :nofile?
How can I account for this 'ghost' file (or if it is detecting the archive folder -- how else can I ignore it)?
Windows kernel and therefore also Windows command interpreter interprets the wildcard pattern *.* like * which means any file or folder. On using wildcard pattern *.* it does not mean there must be a file (or folder) with a dot in name.
For that reason using the condition IF NOT EXIST "%mypath%\*.*" is the same as using IF NOT EXIST "%mypath%\*".
IF EXIST "%mypath%\*" is often used in batch files to verify that %mypath% specifies a folder and not a file because this condition checks if there is a folder %mypath%. The condition is true if that folder exists, independent on number of files and folders in that folder.
So the condition at top of your batch file does not check if there is not at least 1 file in folder %mypath%, it checks if this folder does not exist at all.
You could use the following batch code which avoids the usage of delayed expansion by using a subroutine.
#echo off
for /F "delims=" %%I in ('dir /A-D /B /ON "%mypath%\*" 2^>nul') do call :ProcessFile "%mypath%\%%I"
goto :EOF
:ProcessFile
set "FilenNmeWithExt=%~nx1"
set "FileName=%~n1"
set "FilePath=%~p1"
rem do other filename specific tasks
del "%~1"
goto :EOF
The command FOR executes the command line
dir /A-D /B /ON "%mypath%\*" 2>nul
in a separate command process in background and captures the output of DIR written to handle STDOUT.
DIR would output an error message to handle STDERR if the directory does not exist at all or does not contain any file. This error message is suppressed by redirecting it to device NUL using 2>nul. The redirection operator > must be escaped here with caret character ^ to be interpreted first by Windows command interpreter as literal character on parsing the entire FOR command line as otherwise a syntax error would be the result.
Option /A-D means that DIR should output all directory entries NOT having directory attribute set, i.e. just files, not folders. /B changes output of DIR to bare format which means only the file names without any additional data. /ON results in ordering the list by file name before DIR outputs the entire list. This option would not be really necessary here.
FOR processes now the captured output of DIR. So it does not matter that files from that directory are deleted while FOR is running. FOR processes the initial list as output by DIR.
For each file name output by DIR the subroutine ProcessFile is executed which is like calling another batch file with that name. Passed to the subroutine is the file name with its path. DIR outputs just the file name without path on not using additionally /S to get a list of all file names in specified directory and all its subdirectories.
The command goto :EOF after the FOR loop is required to avoid a fall through to the subroutine once all file names output by DIR have been processed.
The command goto :EOF after the subroutine would not be required if the line above is the last line of the batch file. But it is in general good practice to end a subroutine always with goto :EOF in case of ever adding more command lines like another subroutine below. For Windows command interpreter it does not matter where the subroutine starting with its label is located in the file.
The if exist test looks for anything in the directory.
I'd restructure you code:
:MYLOOP
set "found1="
FOR %%F IN ("%mypath%\*.*") DO (
set filenameWithExt=%%~nxF
set filename=%%~nF
set filepath=%%~pF
set "found1=Y"
)
if not defined found1 goto nofile
do other filename specific tasks
del "%mypath%\%filenameWithExt%"
GOTO MYLOOP
:nofile
If the for finds no files, found1 will remain undefined so we go to the :nofile label, else we have a file to process. Having deleted the file, just go back to the beginning, clear the flag and repeat...
I created the following batch script to create a folder based on today's date and then group files into folders based on the file name.
For example the files
JIM_BRICKMAN_QPS.avi
JIM_BRICKMAN_Slice.avi
JIM_BRICKMAN_Slice.jpg
are moved to the folder BRICKMAN.
This works fine, however, attempts to modify the batch file to move the newly created folders into the newly created date folder fail or overwrite the folders when going through the loop.
for /F "tokens=1-4 delims=/" %%A in ('date /t') do (
set DateDay=%%A
set DateMonth=%%B
set DateYear=%%C
)
set CurrentDate=%DateDay%-%DateMonth%-%DateYear%
if not exist "%CurrentDate%" md %CurrentDate%
for %%A in (*.avi *.jpg) do (
for /f "tokens=1,* delims=_" %%D in ("%%~nA") do (
md "%%D" 2>nul
echo Moving file %%A to folder %%D
move "%%A" "%%D" >nul
)
)
echo Finished
Additionally, I can't seem to get the token to ignore the first delimiter so that the folder is titled JIM_BRICKMAN and not just BRICKMAN.
EDIT:
I rewrote the batch file after the suggestions in the comments:
set "CurrentDate=%DATE:~-10,2%-%DATE:~-7,2%-%DATE:~-4%"
if not exist "%CurrentDate%" md %CurrentDate%
for %%A in (*.avi *.jpg) do (
for /f "tokens=1,2 delims=_" %%D_%%E in ("%%~nA") do (
md "%%D_%%E" 2>nul
move "%%A" "%%D_%%E" >nul
)
)
But the script seems to bomb out. I tried to capture the error, but it closes despite me putting PAUSE in the script.
Double clicking on a batch file in development is no good idea because this results in starting
%SystemRoot%\System32\cmd.exe /c "batch file name with full path and extension"
As it can be read on running cmd /? from within a command prompt window, the option /C means close the command process and its console window immediately after execution of command, executable or script finished independent on the reason for ending the execution.
For debugging a batch file in development it is much better to
open a command prompt window,
change the current directory with command CD to directory of batch file and
run the batch file by typing its name and hitting key RETURN or ENTER.
For batch files which should work independent on which directory is the current directory, it is advisable to omit point 2 and run the batch file with entering its full path, file name and file extension enclosed in double quotes with current directory being not the directory of the batch file.
A batch file is executed from within a command prompt window with:
%SystemRoot%\System32\cmd.exe /K BatchFileNameAsTyped
The option /K means keep command process running which results in keeping also command prompt window opened after execution of command/executable/script which makes it possible to read error messages.
The keys UP ARROW and DOWN ARROW can be used to reload command lines once entered in command prompt window making it easy to run the batch file once again after making a modification in GUI text editor.
And with having #echo off removed from first line of batch file, or changed to #echo ON, or commented out this line with command REM or with :: (invalid label) at beginning, it is also possible to see which lines Windows command interpreter really executes after applying immediate environment variable expansion and where an error occurs in case of a syntax error.
Wrong on second batch code is the line:
for /f "tokens=1,2 delims=_" %%D_%%E in ("%%~nA") do (
Specified as loop variable must be always 1 character. Right would be:
for /f "tokens=1,2 delims=_" %%D in ("%%~nA") do (
The command echo %DATE% outputs on my computer with my account and my region settings today the date 01.04.2017.
The command echo %DATE:~-10,2%-%DATE:~-7,2%-%DATE:~-4% outputs 01-04-2017.
So this part of the script works.
Hint: A list of directories in format YYYY-MM-DD is better than in format DD-MM-YYYY. The list of directories with format YYYY-MM-DD sorted alphabetically as by default is automatically with this date format also sorted from oldest to newest. Date format DD-MM-YYYY results in a weird list of the directories on being sorted alphabetically as by default.
A batch file for this task could be:
#echo off
set "CurrentDate=%DATE:~-10,2%-%DATE:~-7,2%-%DATE:~-4%"
for %%A in (*.avi *.jpg) do (
for /F "tokens=1,2 delims=_" %%D in ("%%~nA") do (
if not "%%E" == "" (
md "%CurrentDate%\%%D_%%E" 2>nul
move /Y "%%A" "%CurrentDate%\%%D_%%E\"
) else (
md "%CurrentDate%\%%D" 2>nul
move /Y "%%A" "%CurrentDate%\%%D\"
)
)
)
set "CurrentDate="
How the inner loop works is most interesting for this task.
for /F and "%%~nA" means the command FOR should process just the file name of the *.avi or *.jpg file without file extension found by outer FOR loop.
delims=_ means the FOR command should split up the string into multiple parts (tokens) using underscore as delimiter. The first file name is JIM_BRICKMAN_QPS which would be split up to:
JIM assigned to loop variable D being specified in FOR command line,
BRICKMAN assigned to loop variable E which is the next character in ASCII table after D and
QPS assigned to loop variable F.
This string split feature is the reason why loop variables are interpreted case-sensitive while environment variables are interpreted not case-sensitive.
With tokens=1,2 is specified that just first and second string parts are of interest. So inner FOR can stop string splitting after having already determined the first two underscore delimited strings and having assigned them to the loop variables D and E.
FOR executes the command block if it could determine at least 1 string delimited by an underscore. So it is possible that loop variable D has a string value, but loop variable E is an empty string, for example if the file name does not contain any underscore. That is the reason for the IF condition.
The command MD creates with command extensions enabled as by default the entire directory tree. Therefore it is not necessary to create the date subdirectory explicitly before searching for *.avi and *.jpg files. That is good as it avoids creating empty date directories when there are no *.avi and *.jpg files in current directory.
As the *.avi and *.jpg files in current directory should be moved to DD-MM-YYYY\Token1_Token2 it is of course necessary to specify also the environment variable with todays date string on creating the directory and moving the file.
The error message output by MD if the directory exists (or when it fails to create the directory because of missing permissions) to handle STDERR is redirected with 2>nul to device NUL to suppress it.
The MOVE command is used with option /Y to move the file to target folder even if the current file exists already in target folder.
For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.
echo /?
for /?
if /?
md /?
move /?
set /?
Read also the Microsoft article about Using Command Redirection Operators.
I want to replace the content paths defined into the file i.e logging.properties to the desired location path of the jboss7 location .
Basically i'm using installer where i have to browse my jboss7 folder and locate it to any desired location of the user . But in few files of jboss7 there are some hardcoded path defined like in given logging.properties file.
I need to change that hard coded path to the desired location path.
As of now i'm having repl.bat and file test.bat files in the same folder.
repl.bat helper file could be find in following link:-
http://www.dostips.com/forum/viewtopic.php?f=3&t=3855
I just copied the code and created repl.bat file.
test.bat file :-
#ECHO OFF
SETLOCAL
SET "folder="
FOR /r "C:\" %%a IN (tintin.txt) do IF EXIST "%%a" SET "folder=%%~dpa"&GOTO got1
FOR /r "D:\" %%a IN (tintin.txt) do IF EXIST "%%a" SET "folder=%%~dpa"&GOTO got1
:got1
echo "%folder%"
PAUSE
set "newpath=%folder%"
set "newpath=%newpath:\=\\%"
echo "%newpath%"
PAUSE
type "logging.properties" | repl "(Directory=).*(\\\\standalone\\\\)" "$1%newpath%$2">"logging.properties.tmp"
PAUSE
move "logging.properties.tmp" "logging.properties"
PAUSE
GOTO :EOF
PAUSE
Here in this test.bat file , i'm searching a file tintin.txt file and setting the path into a variable name as 'folder'. tintin.txt file is just inside the folder of jboss7.This is because of the possibilities of more than one jboss7 application server folder into the system.
Till now i have got the path i.e "C:\Users\Anuj\Desktop\jboss7\" and set into the variable 'folder'.
Now there is file named logging.properties into the folder location
C:\Users\Anuj\Desktop\jboss7\standalone\configuration
logging.properties :-
com.latilla.import.uploadDirectory=C:\\progra~2\\Latilla\\C4i\\jboss7\\ standalone\\uploads
com.latilla.import.maxFilesUploadNumber=10
com.latilla.export.templateFile=C:\\progra~2\\Latilla\\C4i\\jboss7\\standalone\\templates\\GDV_HDI_Format.xls
com.latilla.etl.pluginsRootDirectory=C:\\progra~2\\Latilla\\C4i\\jboss7\\standalone\\cloverETL\\plugins
com.latilla.etl.templatesDirectory=C:\\progra~2\\Latilla\\C4i\\jboss7\\standalone\\etl
com.latilla.db.user=postgres
com.latilla.db.pass=password
repl.bat helper file helps to replace the url path with the desired path i.e path set to variable name 'folder'.
I want to replace the C:\progra~2\Latilla\C4i\jboss7\ with the path set to variable name 'folder'.
Note :-
here in logging.properties file path contents is having different format of path i.e C:\
means double slash. \
Might be the script that i have tried test.bat is incorrect.
When i double click the test.bat file i got error.
Although I can't help you with fixing the issue you are getting while using the repl.bat file, I can suggest a different way of solving the initial problem of path replacement.
If the jboss7 string is guaranteed to be present in all the original paths in your configuration file(s), you could try the following approach:
#ECHO OFF
SETLOCAL DisableDelayedExpansion
FOR /F "delims=" %%A IN ('DIR /B /S C:\tintin.txt') DO (CD /D "%%~dpA" & CALL :got1)
FOR /F "delims=" %%A IN ('DIR /B /S D:\tintin.txt') DO (CD /D "%%~dpA" & CALL :got1)
GOTO :EOF
:got1
SET "propfile=%CD%\standalone\configuration\logging.properties"
IF NOT EXIST "%propfile%" GOTO :EOF
SET "tempfile=%TEMP%\logging.properties.tmp"
FIND /I /V "jboss7\\" >"%tempfile%"
>>"%tempfile%" (
FOR /F "tokens=1,* delims=" %%I IN ('FIND /I "jboss7\\"') DO (
SET "pathname=%%J"
SETLOCAL EnableDelayedExpansion
IF NOT "!pathname!" == "!pathname:*jboss7\\=!" (
SET "pathname=%__CD__:\=\\%!pathname:*jboss7\\=!"
)
ECHO %%I=!pathname!
ENDLOCAL
)
)
ECHO Old file "%propfile%":
TYPE "%propfile%"
ECHO =======================================
ECHO New file:
TYPE "%tempfile%"
PAUSE
:: uncomment the next line once you have verified the replacement works correctly
::MOVE "%tempfile%" "%propfile%"
Searching for the tintin.txt file has been changed slightly so as to possibly make the process faster. Instead of iterating over every directory and checking if it contains the file, the loops now read the output of DIR, which returns only actually existing entries.
Note that you could also use a FOR /R loop, as in your present code, with the same effect i.e. returning only existing paths, but the IN clause would need to contain a mask rather than a normal name, but that would have to be a mask that couldn't match anything else in your system than just tintin.txt. For instance, if you knew for certain that there could be no file called tintin.txt1 or tintin.txtx or anything else where tintin.txt is followed by exactly one character, you could use the following template instead:
FOR /R "C:\" %%A IN (tintin.txt?) DO (CD /D "%%~dpA" & CALL :got1)
and same for D:\. That would return only references to files actually existing and matching the mask.
Also, you can see that the loops do not jump (GOTO) to the got1 label but instead call the got1 subroutine. With that change, it is possible to process many application instances in one go. I don't know yours can be installed multiple times. If not, you'll probably want to change it back to GOTO.
The subroutine in my script is referencing the config file using its full path as specified in your description (...\standalone\configuration\logging.properties). For some reason, in your script the file is referenced simply by its name, even though there's no preceding CD or PUSHD command changing the current directory to the location of the file. I assumed you were trying to simplify your script and omitted that bit, whether intentionally or not. Otherwise I may have missed something in your explanation and/or script.
After verifying that the config file exists at the expected location, the replacement itself is done in this way:
All the non-path config lines are written to a temporary file with one go.
Every config line containing a path is processed in this way:
if it does not contain the jboss7\\ string, it is omitted;
otherwise the part of the path up to and including jboss7\\ is removed;
the current directory is inserted before the remaining part (after every \ is replaced with \\);
the new value is put back into the config line;
the update line is added to the same temporary file.
The old version is of the configuration file replaced with the new one.
Obviously, the script may change the order of lines in the processed file, but it is assumed that that doesn't matter.
e.g. I want to run a windows batch file, say upgrade.bat, which copies a bunch of files from a source directory to the directory that the batch file is in. Problem is, one of the files copied might be a newer version of upgrade.bat, so that the batch file will overwrite itself while its still running.
This seems to result in some unpredictable behavior of batch file execution, so I want to avoid copying over a batch file which is still running. Ideally, I want the existing version of upgrade.bat to run until it is finished, and next time run the new version. Are there any (simple) ways to achieve this ?
#ECHO OFF
SETLOCAL
IF /i NOT "%~dp0"=="%temp%\" (
COPY /y "%~dpnx0" "%temp%\%~nx0" >nul
"%temp%\%~nx0"
)
ECHO Now we run the rest of the original UPGRADE.BAT
This sequence of lines at the start of upgrade.bat should work.
See whether we're running from a copy in %temp%. If not, then copy this file to temp & run it from there.
Hence the batch actually runs from %temp% and the original version may be overwritten.
In order to do that, the following requirements must be satisfied:
The overwrite of the Batch file for the newer version of itself must be the last command of the Batch file, so the next command after the copy must be an exit /B or exit.
Previous commands must be loaded in memory before they are executed. This is easily done by enclosing them in parentheses.
That is:
#echo off
rem Do program business here...
echo Anything
rem Parse/load following commands before execute they:
(
rem Copy many files, probably a newer version of myself
xcopy /Y *.*
rem You may execute other commands here...
echo Files copied!
rem Terminate *this version* of the running Batch file
exit /B
)
You can run the copy as the last operation using the start command to launch it from another terminal. Check this example, specially the last lines.
#echo off
set CUR_FILE=batman.bat
set FOUND_EQUAL="FALSE"
set FROM_DIR=c:\temp\galeria\
SETLOCAL DisableDelayedExpansion
FOR /R %FROM_DIR% %%F IN (*) DO (
SET "p=%%F"
SETLOCAL EnableDelayedExpansion
SET ABC=!p:%FROM_DIR%=!
IF NOT !ABC! == !CUR_FILE! (
echo copying %%F
copy "%%F" .
)
ENDLOCAL
)
echo trying to copy file with the same name [last operation]
start copy "%FROM_DIR%%CUR_FILE%" .
#echo off
cmd /c copy "C:\somepath\upgrade.bat" "%0" & %0
This would start a new cmd process and replace the current batch file with "C:\somepath\upgrade.bat" and restart the batch file
Would it be an option to tag each new version of the batch file with a version suffix and use a second batch file as a launcher?
For example say you are payload batch file is upgrade.bat, subsequent versions will be named as upgrade_001.bat, upgrade_002.bat, upgrade_003.bat .. or upgrade_201305122134 (suffix being yyyymmddHHMM), the new batch file launcher.bat would look for the latest batch upgrade file by looking for the one with the highest suffix and execute it
if NOT "%1"=="RUN" (
copy "%~0" temp_update.cmd
temp_update.cmd RUN
)
Just put this at the top of your cmd file
any name can be used for the temporary file.
Make the batch file copy itself to updateNow.bat and then run updateNow.bat:
So contents of update.bat is:
if %0 == update (
copy update.bat updateNow.bat
updateNow.bat
) else (
copy newUpdate.bat update.bat
)