Pause command if not succeeded instead of automatically closing - windows

Lets say I want to copy a file or run a program, but the file or the location is not found. How can I let my computer pause the command window when these kind of problems occure? I made a batch file to copy files to another location, but also to run programs.
Any help will be appreciated.

In your batch script, use xcopy to do the actual copying, rather than the copy command -
xcopy, unlike copy, returns error codes depending on the result of the copy, which are documented here in the remarks section.
As Aleksandr mentioned, you can use the error codes as part of your script to pause on error.
Assuming you would like to pause on all errors, you could do script this as below:
xcopy /HECY <source> <destination>
if %ERRORLEVEL% NEQ 0 pause
Obviously you'll need to replace and in the above with the locations you are copying from and to respectively.
The /HECY switches are just an example, but in this case could be used to instruct xcopy to copy hidden files, recurse directories, and automatically overwrite files in the destination if they exist. You can tweak these to your specific needs.

Check the error code and pause if it is non zero

Related

Files copied with batch script disappear from folder once terminal is closed

Right now I'm trying to work on a batch script that will take files from one directory, rename them, and copy them into another directory. It's incredibly simple but I'm running into a strange problem where the script will copy and rename files appropriately, but as soon as the terminal closes and the script terminates, the files disappear as if nothing was used.
REM displays as mmddyyyy
set copy_date=%date:~4,2%%date:~7,2%%date:~10,4%
set file_path=C:\wkspace\dir\files\
set archive_path=C:\wkspace\archive\files
Call :copy_files_to_archive
pause
:copy_files_to_archive
set dir_list=\dir1 \dir2 \dir3 \dir4
for %%i in (%dir_list%) do (
if %%i==\dir1 (
copy "%file_path%%%i_filename_%copy_date%.xml" "%archive_path%\sub_dir%%i_filename-ARCHIVED_%copy_date%.xml"
)
if %%i==\dir2 (
... the same code is repeated for the rest of the files through \dir4
)
)
EXIT /B 0
Like I said earlier, the files are showing up in the target directory and they're renamed. But when I hit any key in the terminal to close it, all of the files just disappear. They aren't hidden or anything so I'm stumped.
Just a note: I stripped this code down to only focus on one file because it's repetitive. Focusing on the one file should fix my problem
All help is appreciated! Thanks!!

Batch file flashing command prompt in endless loop after being executed

Background
I want to use ROBOCOPY to backup folders. To learn this, I created a test source folder, containing other subfolders and dummy files.
F:\RoboCopy\RoboCopy_Files
I am able to ROBOCOPY the source folder from the Command line and PowerShell (with using Windows 10).
ROBOCOPY "RoboCopy_Files" "RoboCopy_Files_Testing" /MIR
It does exactly what I want.
Now I put the command into batch file Robocopy.cmd.
Problem symptoms
However, when I put the same command into Robocopy.cmd file, in the root F:\RoboCopy folder and run it, I get only flashing cmd window with my command repeated on ever increasing number of lines.
How can I put the command into a CMD file (e.g. Robocopy.cmd) for later use/share/schedule? How to prevent command prompt from flashing in endless loop without running the command?
Note: I feel this is more about learning how to put cmd scripts into files, than how to use ROBOCOPY.
Cause: File and Command have the same name
I wanted to use the ROBOCOPY command inside the ROBOCOPY file. This is a mistake.
The file ends up calling itself.
Solution 1: Rename batch file
One solution is to rename the batch file to be different from the command(s) in the file.
Solution 2: Use more explicit command call
Another solution could be to use ROBOCOPY.exe or explicitly specify the full path to the exe like C:...\robocopy.exe. This would prevent the "confusion" in calling the command vs calling the batch file itself.
Solution 1 x 2
The best solution (thx Mofi again) is to combine the 1 x 2 together. Use unique batch file name AND specify the full path to command (exe) inside the batch file.
Useful related commands: To determine the full path to command (exe), see the WHERE command (e.g. Where Robocopy.exe - This should be somewhere in windows folder.), system variables (e.g. SS64), or the command SET.
The full version in my case would be to run for example BackupRobocopyFiles.cmd with line:
%SystemRoot%\System32\robocopy.exe "RoboCopy_Files" "RoboCopy_Files_Testing" /MIR /R:2
Note: This would work only if the cmd file is in the root folder F:\RoboCopy. If I would like to run the cmd from different folder (or task sheduler), I would specify the full path to SOURCE_FOLDER and DESTINATION_FOLDER parameters of ROBOCOPY command.
Contributions
The answer was found based on comments by: "MC ND", "eryksun". "Mofi" did point out, that the original Q was not helpful. Thanks to all.

issues in exe files from bat files

ok...Im a new member here and I can express how jolly I am...back to subject
I made a bat file, lets call it 1.bat and I used iexpress to make it an exe file, lets call it 1.exe.
So, in the batch file I added the command line to add a vbs file ( call it 1.vbs ) which is also included in the exe package (1.bat and 1.vbs are in 1.exe) but it installs 1.bat, so in the command I typed:
copy "1.vbs" "C:\Users\%username%\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup"
If I run the bat file it copies the vbs file to start up, but if I make the 1.exe file in iexpress by including 1.bat and 1.vbs and when I click the 1.exe file, it doesnt work, it doesn't copy the vbs file to startup, it says something about too many parameters.
I'm sorry if my post is too long or my question have been posted before, but I couldn't found any thread solves my issues, if there is, i'd be glad to be enlightened.
Thanks.
Never use "Batch to EXE" converter, they often do not work as expected. Simply use the batch script.
Just a note, this file actually only acts as a wrapper for your script, and the script itself actually gets executed in a temp folder created on execution (and deleted afterwards), so make sure you don't use any relative paths.
source
When running a batch file in IExpress, you need to call it like:
cmd /c 1.bat
If you don’t, variables like %username%, long file names, etc, will likely not work.
I suspect you are using short file names. Put in your bat dir then pause and you'll see it's 1~1.vbs or similar.

Listing the files inside a folder using batch file

I am using a batch file to view the contents (xml files) of a certain folder.
I need to use a batch file for it.
Found this command on internet and it is working perfectly when I type in the command prompt, but when I type it in and save as a batch file, it doesn't give any out put at all. (Basically not running the content)
FOR /R D:\Myfolder %F in (*.*) do rename %~nF.xml %~nf1.xml
There are no restrictions in the folder either.
Shouldn't have worked from the prompt.
In a batch file, you need to change any % for the metavariable (%F in this case) to %%.
Having said that however, (or, from the prompt, %F and %f) are two different animals. It's virtually the only situation where batch is case-sensitive. Your command attempts to rename files using their NAME only (~nF) so if it was to encounter fred.txt it would attempt to rename Fred.xml to -er, 1.xml (I think, maybe %~nf.xml)
Best to say what you're trying to do. We're reasonably slick at crufting up solutions...

How to do a for loop in windows command line?

I was wondering if this was possible? I'm not familiar with using windows command line, but I have to use it for a project I'm working on. I have a a number of files, for which I need to perform a function for each. I'm used to working with python, but obviously this is a bit different, so I was hoping for some help.
Basically I need the for loop to iterate through 17 files in a folder, perform a function on each (that's using the specific software I have here for the project) and then that will output a file with a unique name (the function normally requires me to state the output file name) I would suck it up and just do it by hand for each of the 17, but basically it's creating a database of a file, and then comparing it to each of the 17. It needs to be iterated through several hundred times though. Using a for loop could save me days of work.
Suggestions?
The commandline interpreter does indeed have a FOR construct that you can use from the command prompt or from within a batch file.
For your purpose, you probably want something like:
FOR %i IN (*.ext) DO my-function %i
Which will result in the name of each file with extension *.ext in the current directory being passed to my-function (which could, for example, be another .bat file).
The (*.ext) part is the "filespec", and is pretty flexible with how you specify sets of files. For example, you could do:
FOR %i IN (C:\Some\Other\Dir\*.ext) DO my-function %i
To perform an operation in a different directory.
There are scores of options for the filespec and FOR in general. See
HELP FOR
from the command prompt for more information.
This may help you find what you're looking for...
Batch script loop
My answer is as follows:
#echo off
:start
set /a var+=1
if %var% EQU 100 goto end
:: Code you want to run goes here
goto start
:end
echo var has reached %var%.
pause
exit
The first set of commands under the start label loops until a variable, %var% reaches 100. Once this happens it will notify you and allow you to exit. This code can be adapted to your needs by changing the 100 to 17 and putting your code or using a call command followed by the batch file's path (Shift+Right Click on file and select "Copy as Path") where the comment is placed.
You might also consider adding ".
For example for %i in (*.wav) do opusenc "%~ni.wav" "%~ni.opus" is very good idea.

Resources