Link to home
Start Free TrialLog in
Avatar of Doug Van
Doug VanFlag for Canada

asked on

Batch file to copy folders/files with very long paths (how to automatically reduce path lengths?)

I created a batch file to synchronize the multiple "\packaged" folders in multiple directories

In other words, find and copy the following from my Google Drive:

J:\My Drive\Programming TestDept\Operations\\Localization\Drops\2021\20210816 Drop2c (summer)\drop20210816-drop2c\VP\Packaged

J:\My Drive\Programming TestDept\Operations\Localization\Drops\2021\20210911Drop3 (summer)\Van\Packaged

etc.


But the problem is that the paths created are beyond the supported 256 character length for Windows 10. :( 


Here is the batch file:

@echo off
for /d /r "J:\My Drive\Development Enablement\Localization\Drops\2021" %%a in (packaged?) do (
  if /i "%%~nxa"=="packaged" xcopy "%%a" "D:\_System\_loc\Sync\%%~pnxa\" /s/h/e/k/f/c
)


If I remove the "p" in %%~pnxa, the entire path is omitted, which is also not good because I want to at least preserve the path, starting with the date.

In other words, I would like the copied folders in the above examples to be copied like:

\20210816 Drop2c (summer)\drop20210816-drop2c\VP\Packaged 

\20210911Drop3 (summer)\Van\Packaged 


Not

\My Drive\Programming TestDept\Operations\Localization\Drops\2021\20210816 Drop2c (summer)\drop20210816-drop2c\VP\Packaged


Thank you for your advice. :) 



Avatar of oBdA
oBdA

The path length is not much of an issue anymore in a current Win 10; robocopy or PowerShell (if done correctly) don't care.
More of an issue is how the script is supposed to identify the proper target folder.
There doesn't seem to be a consistent pattern here. You're going by the existence of a folder with the name "Packaged", and then in one instance want to copy it to a target determined from three levels up, the next one from two levels up.

\20210816 Drop2c (summer)\drop20210816-drop2c\VP\Packaged
\20210911Drop3 (summer)\Van\Packaged
Avatar of Doug Van

ASKER

Actually, the batch file doesn't seem to care either. However, within File Explorer, navigating to the longest of paths can crash File Explorer.

No, the path lengths are not consistent, but what is consistent is that I would like to always truncate the bold path section from the source:
\My Drive\Programming TestDept\Operations\Localization\Drops\2021\20210816 Drop2c (summer)\drop20210816-drop2c\VP\packaged

The bold section will always be consistent and I would like to not duplicate this unnecessary section on the destination, if possible.
Just as suspected, the above example paths were copied from my personal computer, but from an AWS virtual machine, the folder paths are crazy and over 500 characters. The batch file is continuously failing with an "out of memory" error. :(
Hence, the need for a way to trim down the path length. 
ASKER CERTIFIED SOLUTION
Avatar of oBdA
oBdA

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
oBdA- Absolutely brilliant! Thank you. I am very grateful. :)
 
And thanks to everyone else for your help.