Am having a batch file used to update certain files.
However when i try to delete a file from a specified folder i am getting error "Invalid syntax, directory".
The command is below
del /Q %INSTDIR%\xyz.dll
The %INSTDIR% is taken from registry and is correctly processing any copy commands. But am getting error for del command. Please help
Try this:
del /Q "%INSTDIR%\xyz.dll"
Most likely %INSTDIR% has spaces in it sometimes and thus the failure. You must place double quotes around long file/folder names.
Related
Bear with me here; I'm super new to coding something like this, and I think I've found a simple way to do it, but I'm running into problems. I also couldn't really find an answer from Googling or looking around here, but if it's already been answered, I apologize!
I'm running the following command in a .bat file:
xcopy D:\SHARENAME \\NAS-IP-ADDRESS\SHARENAME /s /e /d
When I run it once as a .bat file, it does its copy operations, and then closes the terminal window, signaling completion. I can verify that a copy operation occurred, since new files do show up in the Backup directory.
However, once I set this .bat file as a Windows Scheduled Task, it never stops running. The copy still occurs one time, but the task does not end. I want it to run every day at 3AM, which is fine, but it never gets the chance to run after its first time, since it never actually stops. Am I missing something to close out this .bat file in order to stop it properly?
The scheduled task does not end because of XCOPY prompts on copying a file existing already in destination directory if it should be overwritten or not. This prompt is not answered by anybody and so XCOPY waits forever. So cmd.exe never ends the batch file execution and for that reason scheduled task also never ends.
The solution is using the command line:
%SystemRoot%\System32\xcopy.exe "D:\SHARENAME" "\\NAS-IP-ADDRESS\SHARENAME\" /C /E /H /K /Q /R /Y
Most important to fix this issue is the option /Y which avoids the overwrite prompt.
Microsoft's xcopy documentation contains currently the information:
By default, you are prompted to overwrite, unless you run xcopy from within a batch script.
This is a wrong information. XCOPY is an executable in system folder of Windows and does not know if being executed by cmd.exe from within a command prompt window or from within a batch script. /Y must be specified on command line or there is an environment variable COPYCMD containing /Y in value to avoid the overwrite prompt. /Y must not be used on command line on using copy being an internal command of cmd.exe on using COPY in a batch script.
The help of XCOPY output on running in a command prompt window xcopy /? explains all other options used above. At bottom of output help can be read:
The switch /Y may be present in the COPYCMD environment variable.
This may be overridden with /-Y on the command line.
The help of command COPY output on running copy /? ends with:
The switch /Y may be present in the COPYCMD environment variable.
This may be overridden with /-Y on the command line. Default is
to prompt on overwrites unless COPY command is being executed from
within a batch script.
The additional third sentence makes the difference regarding to /Y between XCOPY and COPY on usage from within a batch script.
The help of XCOPY contains also the explanation for /S and /E.
/S Copies directories and subdirectories except empty ones.
/E Copies directories and subdirectories, including empty ones.
Same as /S /E. May be used to modify /T.
Same as /S /E. is a confusing description. Many beginners think it is necessary to specify /S and /E to get entire directory structure with empty directories copied. But this is not true. It is meant that just /E is enough to copy an entire directory structure with including empty directories and /S /E is interpreted like just /E. It is superfluous to specify /S (copy without empty directories) in addition to /E (copy with empty directories) on XCOPY command line.
I recommend reading the SS64 - XCOPY documentation which is currently better than Microsoft's documentation.
Very important on copying one or more files and directories with XCOPY into a specified destination directory is specifying the destination path with a backslash at end. This makes it clear for XCOPY that the destination string specifies a directory and not a file. Otherwise it is necessary to specify additionally /I on copying multiple files or an entire directory tree to inform XCOPY that the destination argument should be interpreted as folder path. But /I does not avoid a prompt if a single file is copied with XCOPY and the destination argument does not end with a backslash, see BATCH file asks for file or folder for details. Therefore it is highly recommended to specify destination folder path always with a backslash at end on copying one or more files to a folder using XCOPY.
So my del command deletes two files instead of one only.
My filesystem with the file I wish to delete looks like this (an example):
/executable.exe
/folder/executable.exe
When I run del /s /q "executable.exe", both executables will be deleted. I'm running Windows 10, any way to stop this happening? I can absolutely not change the second executable's name.
The comments to your question are all quite apropos. The solution to your problem is simply to omit the /S switch, as this causes the DEL command to delete from the current directory, plus all of the subdirectories, all the way down to the bottom of the filesystem.
CMD.EXE offers help for most commands; type the command name followed by /? to get the command help (e.g., DEL /?). You can also get the same information by typing HELP followed by the command of interest, e.g., HELP DEL.
You should not use a destructive command (like DEL) without first understanding what the command will do. Read the help; that's why it was provided.
I'm using the following command in a batch file to recursively unzip files into folder with the name same as the zip file.
#echo off
for /R %%I IN (' dir /d /s *.zip ') DO (
"C:\Program Files\7-Zip\7z.exe" x -y -o"%%~dpI\%%~nI" "%%I"
)
I have a zip file such as abc.zip, this zip contains another zip such as mns.zip.
So when I run the bat file, the command unzips all files correctly.
That is, a folder abc is created with contents of abc.zip, and inside this folder mns.zip is unzipped as well to a folder.
Works as I want. However I get a lot of errors with the same message on the command prompt. I want to avoid that and run the bat file without errors.
Here's the error again:
System ERROR: The system cannot find the file specified.
7-Zip [64] 16.02 : Copyright (c) 1999-2016 Igor Pavlov : 2016-05-21
Scanning the drive for archives:
ERROR: The filename, directory name, or volume label syntax is
incorrect. D:\Unzip_folders\New folder\23Nov2015\error_log.17112015\d
Please help me with this.. How do I get rid of these errors?
One thing you could do, assuming the program is working correctly, would be to suppress all the error output.
To do this, you would append > nul 2> nul to the 7zip command. This redirects all warnings and error output to null. If you'd like to only redirect errors, only include nul 2. Note that this probably isn't the best solution, but if you judt want to get rid of errors this will work
I have a requirement of copying few files in to a folder. I am not sure what the folder name will be but I am sure that the folder will be ending with -Distribution.
I have written a command:
xcopy D:\config.dat D:\All_Dist\basic_dist\*-distribution\/R
But I am getting an error message as:
File creation error - The filename, directory name, or volume label syntax is incorrect.
for /d /r "D:\All_Dist\basic_dist" %%a in (*-distribution) do (
copy "d:\config.dat" "%%~fa"
)
Search for the required folder, then copy the file into it.
Since my directories and files are named differently from yours, I tried to match what you are trying to do and I got this to work.
xcopy low\javadeployreg.* \Users\Philip\appData\Local\temp\*-Dist /R/Y
However, if I include the last backslash after the "*-Dist" I get the same error as you but with the added information Unable to create directory - C:\Users\Philip\appData\Local\temp*-Dist so it looks like you just have to remove that last backslash.
the /Y is needed or you will get a prompt for each file you attempt to overwrite a file.
I have the following code in the batch-file:
#echo on
regedit.exe /S abc.reg
If I run it, I get an error.
I searched the google for this and found nothing. Can someone help me please?
I tried:
- removing /S (the I can read (of course) the errormessage
- Run the file as administrator (I'm admin per default)
I am using windows 8.
The erros message: "Cannot import C:\users....abc.reg". Error opening the file. There may be a disk or a file system error.
Both Files are in the same folder.
The problem was, that I handled the %~dp0 in a false way. %~dp0 stands for the default path, but you don't have to set a backslash after it!
If you want to run a .reg file in the same folder as the .bat file, just write it like this:
#echo on
REGEDIT /S "%~dp0ABC.reg"
This will run the file ABC.reg in the same folder.
If you wan't to run something in a subfolder, you have to do it like this:
#echo on
REGEDIT /S "%~dp0SUBFOLDER\ABC.reg"
The brackets ("") are only needed, if there are whitespaces in the pathname.
Much easier way is run it as a Scheduled task.
In this way you can get your batch file to run as admin without annoying password prompts.
For more valuable information, please read here...
http://forums.whirlpool.net.au/archive/2037875
http://social.technet.microsoft.com/Forums/systemcenter/en-US/be854b0a-5d41-4b03-9a84-8fe79732f33c/run-bat-file-to-call-reg-file-in-sccm?forum=configmgrswdist
http://www.myitforum.com/articles/1/view.asp?id=12036
Right click on aix2_mm.bat and select "Run As Administrator", you will get a full screen UAC prompt that you must agree with, then it will work.
If you don't see 'Run As Administrator' as an option, hold the shift key prior to right clicking.
and it wouldn't hurt to make your batch more portable. The %~dp0 parameter will expand to the full path to ABC.reg if it's in the same folder as the .bat file.
#echo on
regedit.exe /S "%~dp0abc.reg"
Be careful the working directory
My bat file always has the code on first line
cd /d %~dp0
It can safely change current directory to your bat file directory