Windows copy command return codes? - windows

I would like to test for the success/failure of a copy in a batch file, but I can't find any documentation on what if any errorlevel codes are returned. For example
copy x y
if %errorlevel%. equ 1. (
echo Copy x y failed due to ...
exit /B
) else (
if %errorlevel% equ 2. (
echo Copy x y failed due to ...
exit /B
)
... etc ...
)

I'd opt for xcopy in this case since the error levels are documented (see xcopy documentation, paraphrased below):
Exit code Description
========= ===========
0 Files were copied without error.
1 No files were found to copy.
2 The user pressed CTRL+C to terminate xcopy.
4 Initialization error occurred. There is not
enough memory or disk space, or you entered
an invalid drive name or invalid syntax on
the command line.
5 Disk write error occurred.
In any case, xcopy is a far more powerful solution. The equivalent documentation for copy does not document the error levels.
As an aside, you may want to rethink your use of the %errorlevel% variable. It has unexpected results, at least in some versions of Windows, if someone has explicitly done something silly like:
set errorlevel=22
In those cases, the actual variable will be used rather than grabbing the actual error level. The "normal" way of doing this is (in decreasing order since errorlevel is a "greater than or equal to" check):
if errorlevel 2 (
echo Copy x y failed due to reason 2
exit /B
)
if errorlevel 1 (
echo Copy x y failed due to reason 1
exit /B
)
In addition, if you are running Win7 or Win Server 2008 or later, you should look into Robocopy, which is now the preferred mass-copy solution.

It might also be worth pointing out that xcopy doesn't always return the error code you expect.
For example when trying to copy multiple files with a wildcard but there are no files to copy you expect a return error code of 1 ("No files were found to copy"), but it actually returns 0 ("Files were copied without error")
C:\Users\wilson>mkdir bla
C:\Users\wilson>mkdir blert
C:\Users\wilson>xcopy bla\* blert\
0 File(s) copied
C:\Users\wilson>echo %ERRORLEVEL%
0

I believe Copy only returns 0 for success or 1 for failure.
XCopy has documented return codes:
0 = Files were copied without error.
1 = No files were found to copy.
2 = The user pressed CTRL+C to terminate xcopy.
4 = Initialization error occurred. There is not enough memory or disk space, or you entered an invalid drive name or invalid syntax on the command line.
5 = Disk write error occurred.

There is also one point I would like to emphasize: xcopy as well as robocopy can only copy files, but they can't rename them.
While looking at the original situation (copy x y, which looks like a rename to me), I have the impression that the copy command still is the only one suitable for this purpose.

Error# Description
0 No error
1 Not owner
2 No such file or directory
3 Interrupted system call
4 I/O error
5 Bad file number
6 No more processes
7 Not enough core memory
8 Permission denied
9 Bad address
10 File exists
11 No such device
12 Not a directory
13 Is a directory
14 File table overflow
15 Too many open files
16 File too large
17 No space left on device
18 Directory not empty
999 Unmapped error (ABL default)

Related

Backup files on Windows 10 computer

I'm supposed to backup a Windows 10 computer. This means copying specific filetypes e.g. .pdf, .doc, .xls, .jpg etc.
I've tried with xcopy but the backup doesn't get complete due to the fact that the computer apparently has folderpaths that are longer than 254 characters.
Therefore I googled, and found Microsofts robocopy, which can handle that particular problem (among others).
Robocopy is a foldercopy utility and not so much a filecopy utility, but with some parameters it should be able to solve the problem.
I created a batch file witch contains these two lines (+ all the rest of the relevant filetypes)
robocopy c:\ d:\B20180602\ *.pdf /s /A-:SH
robocopy c:\ d:\B20180602\ *.doc /s /A-:SH
The odd thing is that it now copies without problems (regarding to foldernames larger than 254 characters), but the final result of this backup is incomplete. There are several subfolders that aren't copied.
Is there anybody that can give me a hint? Thanks in advance.
UPDATE #1 *
After #selbie's advice I ran this one particular command (as Administrator):
robocopy c:\ d:\B20180602\ *.pdf /s /A-:SH
With the following result:
Total Copied Skipped Mismatch FAILED Extras
Dirs : 761465 761351 1 0 113 0
Files : 1233 1233 0 0 0 0
Bytes : 1.464 g 1.464 g 0 0 0 0
Times : 1:58:47 0:00:50 0:00:00 1:57:56
Speed : 30918074 Bytes/sec.
Speed : 1769.146 MegaBytes/min.
Ended : 4. juni 2018 21:32:21
Elevating to Administrator changed a lot. I will make some statistical samples tomorrow and thereby answer whether the problems is solved.
Thanks in advance

How to automatedly confine a batch file run on a single logical processor?

I want to automatedly confine some (imagemagick image processing) batch jobs to run in the background on a single logical processor so as to minimize the impact on performance of other real time applications.
The method I have cobbled together is to use a command line argument as a flag. When the batch file is first started with no command line argument (and no affinity) it will start itself again with affinity set to the last logical processor, adding a dummy argument to indicate the affinity has been set and to proceed with the main body of the batch job:
REM START OF THISBATCHFILE.CMD
IF NOT [%1]==[] GOTO AFFINITY_SET
SET LAST_PROCESSOR=1
FOR /L %%I IN (2,1,%NUMBER_OF_PROCESSORS%) DO SET /A LAST_PROCESSOR*=2
PROCESSOR = %LAST_PROCESSOR%
START /B /AFFINITY %LAST_PROCESSOR% THISBATCHFILE.CMD DUMMY_ARGUMENT
EXIT 0
:AFFINITY_SET
REM Batch Job Starts Here
REM ImageMagick command 1
REM ImageMagick command 2
REM ...
REM ImageMagick command N
REM Batch Job Ends Here
EXIT 0
This seems to be working OK on my 4-core Win8.1 system to confine the batch jobs to the last (4th) logical processor. My question is - is there a 'better' way to do this?
You can control the resources ImageMagick uses in 3 different ways... via the command line as you start a process, via environment variables and via the config files. For your use case, I would suggest you use the environment variables which will be easier than changing every invocation of convert and more flexible than editing your system config files which would always throttle ImageMagick regardless of whether your real-time application is running.
You can see the resources that you can alter, and their current values, by running:
identify -list resource
Output
Resource limits:
Width: 214.7MP
Height: 214.7MP
Area: 17.18GP
Memory: 8GiB
Map: 16GiB
Disk: unlimited
File: 192
Thread: 1
Throttle: 0
Time: unlimited
I suspect you would want to reduce the number of threads (parallelism) and set a smallish amount of time to relinquish the CPU occasionally. You could also alter the memory ImageMagick is allowed to consume.
So, in concrete terms, I would try setting limits just prior to a bunch of ImageMagick commands:
...
...
SET MAGICK_THREAD_LIMIT=1
SET MAGICK_THROTTLE=50
REM Batch Job Starts Here
REM ImageMagick command 1
REM ImageMagick command 2
REM ...
Alternatively, you can specify limits at each invocation of convert like this:
convert -limit thread 1 input.png ... <processing> ... output.png
If you look at the number of files in my initial limits list, you can see it is 192. I can demonstrate what I am talking about by limiting the number files to 2 for the duration of a single command, and the command I use is one that lists the limits...
convert -limit file 2 -list resource info:
Resource limits:
Width: 214.7MP
Height: 214.7MP
Area: 17.18GP
Memory: 8GiB
Map: 16GiB
Disk: unlimited
File: 2 <--- 2 files only
Thread: 1
Throttle: 0
Time: unlimited
So, for the duration of the command that lists how many files I can have open, I have limited the number of open files to 2 and it shows that.

Remove usb drive using cli, on success remove turn off the windows

I wondering to make a batch file that have purposed like this :
when a usb drive that ejected on my notebook is succesfully unmount, I want to make my windows is shutdown.
So, I use RemoveDrive.
Assumed, I mounted my flash drive to E, I use this command
RemoveDrive.exe E: -L
it gives me message like this :
Removing 'My drive'(E:)
success
Now, I was wondering to make a batch file (.bat), when success, it execute 'shutdown / s'. if failed, it gives me just a message error. How come ?
Amy help it so appreciated.
Edit :
based this web : FAQ
it said :
0 - successfully removed a device
1 - device identified but not removed
2 - device not found or parameters are invalid
4 - RemoveDrive.exe located on the drive to remove -> temporary copy
created and executed
so this is my code so far :
#ECHO OFF
set def="0"
SET /P uname=Enter the drive (letter:):
IF "%uname%"=="" GOTO Error
RemoveDrive.exe "%uname%" -L
if %ERRORLEVEL% == def
shutdwon /s
else
echo 'Something have problem'
GOTO End
:Error
ECHO Please enter your drive's name!
:End
if success or failed, it still gives me the syntax of command is inicnorrect message
As per if command syntax and explanation in Command-Line Reference:
if %ERRORLEVEL% == def (
shutdown /s
) else (
echo 'Something have problem'
)

undeletable 0 byte file

I have a file in Wnidows/System32 that has a size of 0 bytes.
It's shown there in explorer and command prompt, bu I can't delete nor overwrite it. the system claims the file would not exist, but it stays there even after reboot.
The problem causes the installation of an app to fail because the installation also fails to overwrite the file.
I tried to delete via console,
I tried checkdisk, fileassassin, unlocker etc. all failed to delete the file.
I would be glad if anyone could help?
The system is a virtual Windows 2008 R2 server
c:\Windows\System32>dir /x msvcp*
Volume in drive C has no label.
Volume Serial Number is ECD7-BEFA
Directory of c:\Windows\System32
13.08.2014 15:17 0 msvcp100.dll
05.11.2012 23:26 661.456 msvcp110.dll
11.09.2013 19:39 614.000 MSVCP1~1.DLL msvcp110_clr0400.dll
14.07.2009 03:41 597.504 msvcp60.dll
4 File(s) 1.872.960 bytes
0 Dir(s) 93.173.768.192 bytes free
c:\Windows\System32>del /F msvcp100.dll
c:\Windows\System32\msvcp100.dll
The system cannot find the file specified.

How to determine Windows version from a batch script for an inactive drive?

How can one get the version of Windows from the shell (command prompt) via a batch script for a drive that does not contain the active OS? I was hoping for some file that I could test, but it turns out things are a little more vague than I'd hoped. This should be able to determine the version of Windows for all NT releases from 2000 to 8.1.
you can load the windows registry from the "inactive OS' Drive" and read the version from it:
this is not tested, but it's something like this:
set SYSTEM_DRIVE=D:
reg load "HKU\ttt" "%SYSTEM_DRIVE%\Windows\System32\config\SOFTWARE"
reg query "HKU\ttt\SOFTWARE\Microsoft\Windows NT\CurrentVersion" /v ProductName
reg unload "HKU\ttt"
I would think a fairly robust method would be to look at the file version metadata for a standard OS file such as %SystemRoot%\system32\winver.exe.
I did the following (from command prompt):
'findstr /i "f.i.l.e.v.e.r.s.i.o.n" kernel32.dll > ver'
The dots are required because the metadata is unicode, and findstr will thus ignore the
zeros that are between the letters (dot being a wildcard for "any single character").
(see: findstr /?). The snippet can be examined in notepad or, on some systems that still support "EDIT", "EDIT /70 VER". The output is still unicode, but it can be "prettied up"
using programming (f/e vbscript). the raw material can be examined "as is":
F i l e V e r s i o n 6 . 1 . 7 6 0 1 . 1 9 1 3 5 ( w i n 7 s p 1 _ g d r . 1 6 0 1 2 1 - 1 7 1 8 ) 2. Googling windows versions yielded: me=4.9, 2000=5.0, xp=5.1, vista=6.0, 7=6.1, 8=6.2, 8.1=6.3, 10=10.0, (then a bunch more while microsoft put out fires, then 11=21H2. (7601 is the "build" number") Above taken from: https://en.wikipedia.org/wiki/List_of_Microsoft_Windows_versions

Resources