Insufficient memory in Windows command line

29.8k views Asked by At

I created a batch file which copies a directory with xcopy for backup reasons. Then I use pkzip in order to zip the backup folder (and email it through a batch file which is working), but I am getting an error message for insufficient memory. I tried to increase the buffer size in command line properties but it didn't work. I also tried to increase the number of buffers but it didn't work either. Any thoughts/solutions?

The reason I am doing this backup routine is that I want these tasks to be automatically done through Windows scheduled tasks.

3

There are 3 answers

3
Thangasivam Gandhi On

I too got this error : Insufficient memory. I found a relevant answer at this link : https://web.archive.org/web/20070214005534/http://www.terminally-incoherent.com/blog/2007/02/05/xcopy-insufficient-memory/

It appears that this message shows up when the fully qualified (ie. with path) name of the copied file is longer than 254 characters which seems to be Windows maximum path length.

In conclusion: xxcopy works, but robocopy works even better.

Finally my batch file looks like this

@echo off
ROBOCOPY "H:\Laptop-Backup" "E:\Laptop-Backup" /s

rem /e: Include directories and sub directories even if empty
REM /s Copy Subdirectories, but not empty ones.
0
Alexander Samoylov On

The standard Windows command line tools like xcopy, rmdir cannot operate with paths longer than MAX_PATH(260 chars). If you want to remove the directory which contains such file which full path is longer than MAX_PATH, or if you want to copy recursively a directory to such a place where the full path to at least one of its files would be longer than MAX_PATH then these command line tools fail.

However you can bypass it with "move" command line tool, because in distinct to xcopy and rmdir(rd) it seems to not iterating the files in the directory one-by-one. This workaround may be not always convenient and requires additional actions, but for the automation/scripting purpose I don't see any other way if you cannot or don't want using special tools a.k.a robocopy. This way works with a directory, but not with a single file. Let me show an example.

Create a directory. Path length: 245 chars

E:\>mkdir E:\veryveryveryveryveryveryveryveryveryveryveryveryveryveryveryveryveryveryveryveryveryveryveryveryveryveryveryveryveryveryveryveryveryveryveryveryveryveryloooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooongdirectory

Thy to create a file inside it so that the file path length is bigger than MAX_PATH. Usual way it won't work.

E:\>echo "content" > E:\veryveryveryveryveryveryveryveryveryveryveryveryveryveryveryveryveryveryveryveryveryveryveryveryveryveryveryveryveryveryveryveryveryveryveryveryveryveryloooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooongdirectory\longlonglonglonglongfilename.txt
The system cannot find the path specified.

Workaround: Create a file(s) with a short path and move the directory containing the file(s) into the long path.

E:\>echo "content" > somedir\longlonglonglonglongfilename.txt

E:\>move somedir E:\veryveryveryveryveryveryveryveryveryveryveryveryveryveryveryveryveryveryveryveryveryveryveryveryveryveryveryveryveryveryveryveryveryveryveryveryveryveryloooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooongdirectory\
        1 dir(s) moved.

Check 1: Was the directory completely moved (with all files)? - Yes.

E:\>dir E:\veryveryveryveryveryveryveryveryveryveryveryveryveryveryveryveryveryveryveryveryveryveryveryveryveryveryveryveryveryveryveryveryveryveryveryveryveryveryloooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooongdirectory\somedir
 Volume in drive E is Workspace
 Volume Serial Number is C864-7C96

 Directory of E:\veryveryveryveryveryveryveryveryveryveryveryveryveryveryveryveryveryveryveryveryveryveryveryveryveryveryveryveryveryveryveryveryveryveryveryveryveryveryloooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooongdirectory\somedir

08/09/2019  11:52 AM    <DIR>          .
08/09/2019  11:52 AM    <DIR>          ..
08/09/2019  11:52 AM                12 longlonglonglonglongfilename.txt
               1 File(s)             12 bytes
               2 Dir(s)  130,574,221,312 bytes free

Check 2. The path is really longer than MAX_PATH, that's why it won't be accessible by the full path for the usual command line tools, so we did everything correct:

E:\>dir E:\veryveryveryveryveryveryveryveryveryveryveryveryveryveryveryveryveryveryveryveryveryveryveryveryveryveryveryveryveryveryveryveryveryveryveryveryveryveryloooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooongdirectory\somedir\longlonglonglonglongfilename.txt
 Volume in drive E is Workspace
 Volume Serial Number is C864-7C96

 Directory of E:\veryveryveryveryveryveryveryveryveryveryveryveryveryveryveryveryveryveryveryveryveryveryveryveryveryveryveryveryveryveryveryveryveryveryveryveryveryveryloooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooongdirectory\somedir

File Not Found

The same workaround is valid if you want to remove (with rmdir) the directory containing subfolders/files whose path is longer than MAX_PATH. Just move the upper directory to some short temporary location and there you can rmdir it.

NOTE: Windows command line tool "move" can move directories only within the same disk letter.

0
JAGreen On

For me, the issue was that xcopy was trying to copy files with very long filenames inside a particular directory (call it /Documents/LongNames) containing lots of these types of names. Because the files with the long names all went together in my case, I zipped the Documents/LongNames sub-directory to a single zip file, since I do not use it often. The backup with xcopy of all of my files in /Documents works just fine now. In general a good way to debug a big batch xcopy is to have your batch file make a log of the files that it is copying. So when there is an issue, it is easier to find. This is how I found the issue to begin with.