I'm trying to use Robocopy to replace a cmd file - windows

I've created a cmd file which uses the Robocopy command to update some files on the PC, but I can't replace the cmd files, because this contains the Robocopy script which is doing the updating. How do you replace a file which is doing the replacing?
I've moved the cmd file to another directory, which allows me to update most of the files, but I still can't replace the cmd file.
The Flags I'm using in Robocopy are /MIR /Copy:DAT /DCOPY:T
The Robocopy stopped at the cmd file and I can't replace it.

I don't see any reason for %SystemRoot%\System32\robocopy.exe failing to copy the batch file currently processed by cmd.exe than this batch file is additionally opened in an application like a text editor which prevents write access and deletion of the file as long as it is opened in the application.
However, the following code added to your batch file with unknown content could solve the problem.
#echo off
if /I not "%~dp0" == "%TEMP%\" (
copy /Y "%~f0" "%TEMP%" >nul 2>&1
if exist "%TEMP%\%~nx0" (
set "CurrentDirectory=%CD%"
set "InitialExecution=%~dp0"
cd /D "%TEMP%"
"%TEMP%\%~nx0" %*
)
)
rem Insert here other commands to execute by the batch
rem file now running from directory of temporary files.
rem The next three commands are only for demonstration.
if defined CurrentDirectory echo Initial current directory: %CurrentDirectory%
if defined InitialExecution echo Initial execution path: %InitialExecution%
pause
set "InitialExecution="
if defined CurrentDirectory set "CurrentDirectory=" & cd /D "%CurrentDirectory%" 2>nul & (goto) 2>nul & del "%~f0"
This batch file first checks if it is started from directory for temporary files. This is not the case on double clicking on the batch file, except the batch file is stored by chance in directory for temporary files by the user and double clicked on it in this directory. If batch file is not stored in directory for temporary files, it does following:
The batch file copies itself to directory of temporary files (only read access).
It verifies if the file copy was really successful which should be always true.
It defines two environment variables with path of current directory and initial execution path for later usage.
It sets the current directory to directory for temporary files.
This makes it possible to even delete the directory containing batch file on batch file directory being also current directory as typical on double clicking on a batch file stored on a local drive executed by current user.
The batch file runs itself from within directory for temporary files with passing all arguments passed to the batch file on initial execution further on its copy.
The Windows command processor cmd.exe executing the batch file continues batch file processing on its copy in temporary files directory with first line #echo off returning never to the initial batch file as started by the user.
Now with batch file processing done on a copy of initial batch file in temporary files directory and with current directory being also the directory for temporary files, the other commands in batch file can do everything in initial current directory respectively initial execution directory of the batch file like updating the files in these directories or even deleting these directories temporarily or permanently.
The three comment lines with command rem and the next three lines just demonstrate what can be done here and how to use the environment variables set by the batch file on initial execution. The two environment variables do not exist (most likely) on batch file being initially stored in directory for temporary files and executed by the user from this directory.
The batch file deletes the environment variable InitialExecution independent on its existence to restore initial environment in case of batch file executed from within a command prompt window.
Finally with batch file initially not executed from temporary files directory it deletes also the environment variable CurrentDirectory, changes the current directory back to initial current directory, if that directory still exists, and deletes itself from directory for temporary files.
(goto) 2>nul & del "%~f0" for batch file deletion without any error message output by Windows command processor was copied by me from Dave Benham's answer on How to make a batch file delete itself?
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.
call /? ... explains %~dp0, %~f0 and %~nx0
cd /?
copy /?
del /?
echo /?
goto /?
if /?
pause /?
set /?
See also the Microsoft article about Using command redirection operators.

Related

Batch Script to copy files from a Text file

I need help with below code as it is executing in root folder only whereas I want the code to look for files within sub folders as well.
for /F "tokens=*" %%A in (documents.txt) do (
copy %%A E:\Destination\
)
I suggest to use this command line in the batch file to copy all files with duplicating directory structure from source to destination directory.
for /F "eol=| delims=" %%I in (documentation.txt) do %SystemRoot%\System32\robocopy.exe "%~dp0." "E:\Destination" "%%~I" /S /NDL /NFL /NJH /NJS
It is assumed that the file documentation.txt contains a list of file names without path.
The command FOR reads one line after the other from file documentation.txt with skipping empty lines. The end of line character is modified from default ; to | using option eol=| to be able copying also files of which name starts unusually with a semicolon. No file name can contain a vertical bar anywhere. The line splitting behavior on spaces/tabs is disabled by using option delims= which defines in this case an empty list of string delimiters. Therefore file names with one or more space even at beginning of file name read from file are assigned unmodified to loop variable I. The option tokens=* removes leading spaces/tabs from the lines read from text file. A file name can begin with one or more spaces although such file names are unusual.
FOR runs for each file name the executable ROBOCOPY with directory of the batch file as source folder path and E:\Destination as destination folder path. ROBOCOPY interprets a \ left of one more \ or " as escape character. For that reason the source and destination folder paths should never end with a backslash as this would result in " being interpreted not as end of folder path, but everything up to next " in command line. For that reason . is appended to %~dp0 because of %~dp0 always expands to batch file folder path ending with a backslash. The dot at end of batch file folder path references the current folder of batch file folder. In other words with batch file stored in C:\Temp the batch file folder can be referenced with C:\Temp\ as done with %~dp0 but not possible with ROBOCOPY or with C:\Temp\. as done with %~dp0. or with just C:\Temp or with C:\Temp\\ as done with %~dp0\ which would also work with ROBOCOPY. See the Microsoft documentation about Naming Files, Paths, and Namespaces for details.
Remove %~dp0 to use current folder as source folder instead of batch file folder.
The ROBOCOPY option /S results in searching in source folder and all its subfolders for the file and copy each found file to destination folder with duplicating the source folder structure in destination folder.
The other ROBOCOPY options are just for not printing list of created directories, list of copied files, header and summary.
Here is an alternative command line for this task copying all files from source directory tree into destination directory without creating subdirectories. So all copied files are finally in specified destination directory.
for /F "eol=| delims=" %%I in (documentation.txt) do for /F "delims=" %%J in ('dir "%~dp0%%~I" /A-D-H /B /S 2^>nul') do copy /B /Y "%%J" "E:\Destination\" >nul
The inner FOR starts for each file name assigned to loop variable I of outer FOR one more command process in background with %ComSpec% /c and the DIR command line appended as additional arguments. So executed is for each file name in documentation.txt with Windows installed into C:\Windows for example:
C:\Windows\System32\cmd.exe /c dir "C:\Batch File Path\Current File Name.ext" /A-D-H /B /S 2>nul
The command DIR executed by second cmd.exe in background searches
in directory of the batch file
and all its subdirectories because of option /S
only for non-hidden files because of option /A-D-H (attribute not directory and not hidden)
with the specified file name
and outputs in bare format because of option /B
just the names of the found files with full path because of option /S.
It is possible that DIR cannot find a file matching these criteria at all in which case it would output an error message to handle STDERR of the background command process. This error message is suppressed by redirecting it with 2>nul to device NUL.
Read the Microsoft article about Using command redirection operators for an explanation of 2>nul. The redirection operator > must be escaped with caret character ^ on FOR command line to be interpreted as literal character when Windows command interpreter processes this command line before executing command FOR which executes the embedded dir command line with using a separate command process started in background.
The inner FOR captures everything written to handle STDOUT of started background command process and processes this output line by line after started cmd.exe terminated itself after finishing execution of DIR.
The inner FOR assigns each full qualified file name to specified loop variable J without any modification because of option delims= and runs next the command COPY to copy that file as binary file to destination directory with automatically overwriting an existing file in destination directory with same file name. The success message output by COPY to handle STDOUT is redirected with >nul to device NUL to suppress it. An error message would be output by COPY. An error occurs if destination directory does not exist, or the destination directory is write-protected, or an existing file with same name is write-protected due to a read-only attribute or file permissions, or source file is opened by an application with sharing read access denied, or an existing destination file is opened by an application with sharing write access denied.
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.
call /? ... for an explanation of %~dp0 referencing drive and path of argument 0 which is the full qualified path of the batch file currently processed by cmd.exe.
copy /?
dir /?
for /?
robocopy /?

Iterate folder and get filename for commandline input

I am trying to iterate files in a folder and process them with another batch file inside the do loop. It works with echo but as soon as I use the variable as input to the program, it echoes the () part and everything inside.
Here's what I'm trying to do.
#echo off
SETLOCAL ENABLEEXTENSIONS ENABLEDELAYEDEXPANSION
for /r %%f in (/folder/*) do (
set name="%%~nf"
echo !name! <--- ok
process.bat !name! <--- echoes () and commands inside this do loop
)
ENDLOCAL
The process.bat just capitalizes the first letter of the filename and echoes it for debug or confirmation.
A batch file must be called from within a batch file using command call as otherwise Windows command processor continues processing on other batch file with never returning back to initial batch file.
See also: How to call a batch file that is one level up from the current directory?
Please read excellent answer on batch file echo on/off not working properly written by dbenham for the reason on getting suddenly the commands executed by FOR output after first execution of process.bat without using command CALL. I cannot explain better what happens in this case.
The directory separator on Windows is the backslash character \ and not the forward slash / as on Linux or Mac. Windows supports also / in file/folder paths for compatibility reasons by automatically replacing all / by \ before accessing the Windows file systems, but a good written script uses 100% correct syntax and does not depend on automatic corrections done by other programs. / is used on Windows mainly for command line switches.
The usage of / instead of \ can result in an unexpected behavior. For example run a batch file with following content:
#echo off
echo Files in directory %SystemRoot:\=/%/:
for %%I in (%SystemRoot:\=/%/*) echo %%I
echo/
pause
echo/
echo Files in directory %SystemRoot%\:
for %%I in (%SystemRoot%\*) echo %%I
echo/
pause
The first FOR using C:/Windows/* as wildcard pattern outputs the file names with just drive letter + colon + file name + file extension. The file path \Windows\ is missing in output file names. The second FOR loop using C:\Windows\* as wildcard pattern outputs the full qualified file names, i.e. drive letter + colon + file path + file name + file extension.
A file/folder path starting with \ references a directory or file relative to root directory of current DRIVE.
This is explained by the Microsoft documentation Naming Files, Paths, and Namespaces.
It looks like folder is a subdirectory in directory of the executed batch file. In this case / or \ at beginning of folder path is definitely not correct. The backslash at beginning can be omitted or .\ is used to reference the directory folder in current directory on execution of the batch file. But the current directory on batch file execution can be also different to directory containing the executed batch file, for example on running the batch file as administrator, or on running the batch file as scheduled task, or on running the batch file from a network resource accessed using a UNC path. For that reason it is advisable to reference explicitly subdirectory folder in directory of the batch file.
Delayed environment variable expansion is not needed as long as the file name assigned currently to the loop variable does not need to be modified other than the modifiers of for support it. A command line like set name="%%~nf" does not work correct with enabled delayed expansion and file name contains one or more ! because of cmd.exe interprets the exclamation mark(s) in file name as beginning/end of a delayed expanded environment variable reference.
See also: How does the Windows Command Interpreter (CMD.EXE) parse scripts?
It looks like a recursive search for non-hidden files is not really needed as otherwise passing just file name without path and file extension would be not enough to get the right file processed by other batch file process.bat.
So the entire task can be done most likely also with:
#echo off
setlocal EnableExtensions DisableDelayedExpansion
for %%I in ("%~dp0folder\*") do call "%~dp0process.bat" "%%~nI"
endlocal
But if the other batch file process.bat expects that the passed file name without file extension and path is in current directory on execution of process.bat, it is necessary to make the subdirectory folder in directory of this batch file first the current directory.
#echo off
setlocal EnableExtensions DisableDelayedExpansion
pushd "%~dp0folder"
for %%I in (*) do call "%~dp0process.bat" "%%~nI"
popd
endlocal
Note: The batch file folder path referenced with %~dp0 always ends with a backslash. Therefore no additional backslash should be used on concatenating this path string with a file/folder name to avoid having finally on execution of the batch file \\ in full qualified file/folder name, although Windows kernel corrects such paths also automatically by removing second backslash in this case.
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.
call /?
echo /?
endlocal /?
popd /?
pushd /?
set /?
setlocal /?

How to recursively delete all folders of a folder tree unless they contain a file with certain file extension?

How to go though a directory tree and delete all directories unless they contain a file with a particular file extension?
I tried Robocopy thinking the folders were empty. But all the folders have hidden files. So I need something that will take every folder in a directory that does not have a .pdf for example in it and delete it.
The task is to delete all directories/folders not containing a PDF file and also not containing a subdirectory/subfolder containing a PDF file. Let us look on an example to better understand the directory/folder deletion task.
The directory C:\Temp contains following subfolders and files:
Folder 1
Subfolder A
File 1A.txt
Subfolder B
File 1B.txt
Subfolder C
File 1C.pdf
File 1.cmd
Folder 2
Subfolder A
Subfolder B
File 2B.pdf
Subfolder C
File 2C.pdf
File 2.jpg
Folder 3
Subfolder A
File 3A.log
Subfolder B
File 3.doc
Last Folder & End
Subfolder A
Last File A.xls
Subfolder B
Subfolder C
Last File C.pdf
A folder is formatted bold. A hidden folder is formatted bold and italic. A hidden file is formatted italic.
The wanted folders and files after running the batch file should be:
Folder 1
Subfolder C
File 1C.pdf
File 1.cmd
Folder 2
Subfolder B
File 2B.pdf
Subfolder C
File 2C.pdf
File 2.jpg
Last Folder & End
Subfolder C
Last File C.pdf
This result can be achieved by executing following batch file:
#echo off
goto MainCode
:ProcessFolder
for /F "delims=" %%I in ('dir "%~1" /AD /B 2^>nul') do call :ProcessFolder "%~1\%%I"
if exist "%~1\*.pdf" goto :EOF
for /F "delims=" %%I in ('dir "%~1" /AD /B 2^>nul') do goto :EOF
if /I "%~1\" == "%BatchFilePath%" goto :EOF
rd /Q /S "%~1"
goto :EOF
:MainCode
setlocal EnableExtensions DisableDelayedExpansion
set "BatchFilePath=%~dp0"
if exist "C:\Temp\" cd /D "C:\Temp" & call :ProcessFolder "C:\Temp"
endlocal
Doing something recursively on a directory tree requires having a subroutine/function/procedure which calls itself recursively. In the batch file above this is ProcessFolder.
Please read the answer on Where does GOTO :EOF return to? The command goto :EOF is used here to exit the subroutine ProcessFolder and works only as wanted with enabled command extensions. FOR and CALL as used here require also enabled command extensions.
The main code of the batch file first enables explicitly the command extensions required for this batch file and disables delayed environment variable expansion to process correct also folders with an exclamation mark in name. This is the default environment on Windows, but it is better here to explicitly set this environment because the batch file contains the command RD with the options /Q /S which can be really very harmful on execution from within wrong environment or directory.
The subroutine ProcessFolder is not at end of the batch file as usual with a goto :EOF above to avoid an unwanted fall through to the command lines of the subroutine after finishing the entire task. For safety reasons the subroutine is in the middle of the batch file. So if a user tries to execute the batch file on Windows 95/98 with no support for command extensions nothing bad happens because first goto MainCode is executed successfully as expected, but SETLOCAL command line, calling the subroutine and last also ENDLOCAL fail and so no directory was deleted by this batch file designed for Windows with cmd.exe as Windows command processor instead of command.com.
The main code sets also current directory to the directory to process. So C:\Temp itself is never deleted by this code because of Windows prevents the deletion of a directory which is the current directory of any running process or contains a file opened by a running process with file access permission set to prevent other processes to delete the file while being opened by the process.
Next is called subroutine ProcessFolder with argument C:\Temp to process this folder recursively.
Last the initial environment is restored which includes also initial current directory on starting the batch file if this directory still exists.
The command for /D is usually used to do something on all subdirectories of a directory. But this is not possible here because FOR always ignores directories and files with hidden attribute set. For that reason it is necessary to use command DIR to get a list of all subdirectories in current directory including directories with hidden attribute set.
The command line dir "%~1" /AD /B 2>nul is executed by FOR in a separate command process started with cmd.exe /C in background. This is one reason why this batch file is quite slow. The other reason is calling the subroutine again and again which cause internally in cmd.exe to save and restore environment again and again.
Please read the Microsoft article about Using Command Redirection Operators for an explanation of 2>nul. The redirection operator > must be escaped with caret character ^ on FOR command line to be interpreted as literal character when Windows command interpreter processes this command line before executing command FOR which executes the embedded dir command line with using a separate command process started in background.
For each subdirectory in a directory the subroutine ProcessFolder calls itself. The first FOR loop in the subroutine is left if a directory does not contain one more subdirectory.
Then the subroutine checks in current directory if there is at least one *.pdf file. The IF condition used here is true even if the directory contains only a hidden PDF file. In this case the subroutine is exited without doing anything as this directory contains definitely a PDF file and therefore must be kept according to the requirements of the folder deletion task.
Next is checked if the current directory still contains at least one subdirectory as in this case the current directory must be also kept as one of its subdirectories contains at least one PDF file.
Last the subroutine checks if the current directory contains by chance the batch file as this directory must be also kept to finish the processing of the batch file.
Otherwise the current directory is deleted with all files on not containing a PDF file and no subdirectories and also not the currently running batch file as long as Windows does not prevent the deletion of the directory because of missing permissions or a sharing access violation.
Please note that the batch file does not delete other files in a directory which is not deleted as it can be seen also on the example.
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.
call /?
cd /?
dir /?
echo /?
endlocal /?
for /?
goto /?
if /?
rd /?
setlocal /?

Change batch file directory in system32 to CMD command execution point

So basically I have a .bat that is inside my System32 folder.
This batch file accepts a parameter input, this input is a file.
I wanted it to be that I could open my Command Prompt, and for example do
batchfile text.txt
And it would pass test.txt into batchfile.bat. Obviously for my terminal to do this it needs to be in System32.
That is where my issue is. Because the batch file is in System32, when executing the command, it changes my directory to System32.
However the parameter I give it is a file. And when the command is executed and it changes the directory to System32, obviously it can no longer access the file.
How can I get around this?
Before you change directories, expand the filename to its fully-qualified filename and then reference it that way:
#echo off
for %%F in (%1) do set USR_REQFNM=%%~dpnxF
C:
cd \Windows\System32
echo The requested file is "%USR_REQFNM%"
goto ENDIT
REM Cleanup
:ENDIT
set USR_REQFNM=

Force batch file to load to RAM before running

I have a batch file in an administrative partition of my portable drive, with a shortcut symlinked to it on the root of the drive. The purpose of the file is to unmount the drive and remount it as the specified letter (mostly for convenience).
When the file is opened, it is opened relative to the current letter rather than to the volume ID, so naturally, when the unmount happens, the command processor has no idea what to do next as it reads the file as needed rather than caching it.
There are two foreseeable solutions that I can think of but can't figure out:
Make the file get cached into RAM before executing
Make the file run relative to the volume ID instead of the mountpoint (tried using {VOLID}\file where {VOLID} is the volume ID, but it couldn't find the file although it was there (navigating to {VOLID}\ correctly opened the directory, but trying to open the file didn't correctly open the file.
Despite of the other answers, it's trivial to cache a whole batch script to RAM.
You only need to build a single block, as blocks are parsed and cached before they can be executed.
But blocks have some drawbacks, percent expansion doesn't work, therefore you need to use delayed expansion.
call and goto can't be used, as they would try to read from the file again.
(goto) 2>nul & (
echo The script is started
REM Need to change the directory, else the unmount doesn't work
c:
mountvol e: /p
mountvol g: \\?\Volume{VOLID}\
dir G:\
echo The script will end now
REM Here you need the goto 2>nul hack to avoid an error message
)
The (goto) 2>nul & seems strange here, but it's explained at SO:How to make a batch file delete itself?.
It works also without the goto, but then the scripts ends with an error message
Have the batch file determine where it is running from see this. If it's running from the portable drive have it make a copy of itself to a permanent drive location (c:\temp for instance) then run that copy of the batch file.
When running a bath file there is no concept of running it from RAM. Windows command processor will always go back to the .bat file for the 'next' command to run. If you edit a batch file while it's running the command processor will pick up your changes.
JJF wrote already the correct answer. It is not possible to copy a batch file to RAM and inform Windows command interpreter to interpret the command lines in memory. It would be possible to create a RAM disk, copy the batch file to the RAM disk and run it from there. But this just makes the task more complicated than necessary.
This commented batch code demonstrates how to copy a batch file to directory for temporary files and start it there for complete processing in a separate Windows command process.
#echo off
rem Is the batch file path not the path of directory for temporary files?
if /I not "%~dp0" == "%TEMP%\" (
rem Copy the batch file to directory for temporary files.
copy "%~f0" "%TEMP%" >nul
rem Run the copy in a separate command process with name of the batch
rem file with extension as window title and exit this batch process.
start "%~nx0" "%TEMP%\%~nx0"
goto :EOF
)
echo The batch file is now running from directory for temporary files.
echo.
pause
rem Delete the batch file in directory for temporary files
rem and exit the command process started for this batch file.
del "%TEMP%\%~nx0" & exit
Replace the two echo commands and the pause command by your 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.
call /? ... explains %~dp0 (drive and path of argument 0 which is the batch file itself) and %~nx0 (name and extension of batch file)
copy /?
del /?
echo /?
exit /?
goto /?
if /?
pause /?
rem /?
start /?
See also answer on Single line with multiple commands using Windows batch file for an explanation of operator & used here two run the two commands del and exit read from one line to avoid an opened console window with an error message as batch file deleted unexpected for the Windows command interpreter while processing it.

Resources