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


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 


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

Thank you for your advice. :) 

Windows 10Windows BatchMicrosoft DOSPowershell

Avatar of undefined
Last Comment
Doug Van

8/22/2022 - Mon

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
Doug Van

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.
Doug Van

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. 
Your help has saved me hundreds of hours of internet surfing.

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
See how we're fighting big data
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
Doug Van

oBdA- Absolutely brilliant! Thank you. I am very grateful. :)
And thanks to everyone else for your help.