Link to home
Create AccountLog in
Avatar of cmp119
cmp119Flag for United States of America

asked on

xxcopy batch file to move specific client folders using an input text file.

I am in the testing phase right now, so I am using local test source folders.  The F:\xxcopy\NAS\FROM\ folder has (A,B,C,D,E - Z) subfolders, and within each letter folder there are specific client folders that will be listed in an input file:  ClientFolders.txt.

Sample ClientFolders.txt
F:\xxcopy\NAS\FROM\I\INYONG*
F:\xxcopy\NAS\FROM\1-9\724REA*

The problem with the client folders is the name is composed of the client ID (i.e. INYONG, 724REA) which should be the first six characters of the folder name.  After the folder name sixth character there is additional client descriptive text.  So that is why I appended the "*" afterwards.  The first six characters of the folder (Client ID) will always be unique.

I simply want the batch file to use the input text file that has the first six characters of folder names that need to be located and moved.  

So I created the following batch file using xxcopy:

text-copy.bat

for /f "delims=" %%i in (ClientFolders.txt) do echo D|xxcopy "F:\xxcopy\NAS\FROM\%%i" "F:\xxcopy\NAS\TO\%%i" /SC /F /S /E /VL /YY /V /JV /RC /TTA0 /TCA /H /TCC /TCW /I /FW /oA:"F:\xxcopy\29MAR2016 TEST Folder\Logs\Test1.log" /oF3

The above log file reveals the following errors: Exit code 34 (illegal command parameter.  Invalid destination directory name.

===============================================================================
XXCOPY   Pro Edition   ver 3.21.1   2016-03-29 18:44:40   Windows Ver 5.2.3790 Service Pack 2
 Command Line  = xxcopy "F:\xxcopy\NAS\FROM\F:\xxcopy\NAS\FROM\I\INYONG*" "F:\xxcopy\NAS\TO\F:\xxcopy\NAS\FROM\I\INYONG*" /SC /F /LTREE /VL /YY /V /JV /RC /TTA0 /TCA /H /TCC /TCW /I /FW /oA:"F:\xxcopy\29MAR2016 TEST Folder\Logs\Test1.log" /oF3
Invalid destination directory name

 Directories Processed = 0
 Total Data in Bytes   = 0
 Elapsed time in sec.  = 0.000
 Action speed (MB/min) = 0.0
 Files Selected        = 0
 Log File Appended     = "F:\xxcopy\29MAR2016 TEST Folder\Logs\Test1.log"
 Exit code             = 34 (Illegal command parameter)


===============================================================================
XXCOPY   Pro Edition   ver 3.21.1   2016-03-29 18:44:40   Windows Ver 5.2.3790 Service Pack 2
 Command Line  = xxcopy "F:\xxcopy\NAS\FROM\F:\xxcopy\NAS\FROM\1-9\724REA*" "F:\xxcopy\NAS\TO\F:\xxcopy\NAS\FROM\1-9\724REA*" /SC /F /LTREE /VL /YY /V /JV /RC /TTA0 /TCA /H /TCC /TCW /I /FW /oA:"F:\xxcopy\29MAR2016 TEST Folder\Logs\Test1.log" /oF3
Invalid destination directory name

 Directories Processed = 0
 Total Data in Bytes   = 0
 Elapsed time in sec.  = 0.000
 Action speed (MB/min) = 0.0
 Files Selected        = 0
 Log File Appended     = "F:\xxcopy\29MAR2016 TEST Folder\Logs\Test1.log"
 Exit code             = 34 (Illegal command parameter)
Avatar of Arana (G.P.)
Arana (G.P.)

xxcopy doesnt allow wildcards in the destination specifier, but you could just leave it without the %%i  in the destination part and i think it would work just fine, try it with a small set first
Assuming ClientFolders.txt looks like:
1234567890
1234567890
1234567890

Open in new window


Make a test.bat to see what this example does:
@echo off
setlocal enabledelayedexpansion

set FNClients=c:\ClientFolders.txt

for /f %%a in (%FNClients%) do (
  set FNNew=%%a
  ECHO !FNNew:~0,6!
)

Open in new window


When you're satisfied with that, make a similar adjustment for yours:
setlocal enabledelayedexpansion

set FNClients=c:\ClientFolders.txt
for /f %%a in (%FNClients%) do (
  set Client=%%a
  set Client=!Client:~0,6!
  echo D|xxcopy "F:\xxcopy\NAS\FROM\%Client%" "F:\xxcopy\NAS\TO\%Client%" /SC /F /S /E /VL /YY /V /JV /RC /TTA0 /TCA /H /TCC /TCW /I /FW /oA:"F:\xxcopy\29MAR2016 TEST Folder\Logs\Test1.log" /oF3
)

Open in new window

Avatar of cmp119

ASKER

NVIT - that did "not" work.  It moved "all" the files regardless of folder name.  That's not what is needed.  I need to move the client subfolders based on the first six characters of folder name.

The main path = F:\xxcopy\NAS\FROM

Within the \FROM folder there are subfolders such as A,B,C,D,E all the way through Z, also a folder named "1-9".  This was done because there are so many clients to hold in one folder they had to be separated.  

Subfolder layout example:

F:\xxcopy\NAS\FROM\1-9
F:\xxcopy\NAS\FROM\A
F:\xxcopy\NAS\FROM\B
F:\xxcopy\NAS\FROM\C
F:\xxcopy\NAS\FROM\D
...
F:\xxcopy\NAS\FROM\Z

So for testing purposes I wanted to move only two client folders that are located in subfolders I and 1-9 as displayed below.  No other clients folder should be touched other than the partial folder names in the input text file.

F:\xxcopy\NAS\FROM\I\INYONG Inyong corp
F:\xxcopy\NAS\FROM\1-9\724REA 724 Rocky Roads Inc

the ClientFolders.txt simply listed two items:

INYONG
724REA

The first six characters of each client subfolder starts with the Client ID which is unique to each folder. So it should only locate and move two client folders "INYONG Inyong corp" and "724REA 724 Rocky Roads Inc".  

Maybe the ClientsFolders.txt file needs to have the whole path along with the client folder name, such as.  Remember, the clients folders are located in different folders A-Z, 1-9.

F:\xxcopy\NAS\FROM\I\INYONG
F:\xxcopy\NAS\FROM\1-9\724REA
Try this version 2.
Note: This version just ECHOs the command. To run it for real, remove the ECHO prefix on line 17:
@echo off
setlocal enabledelayedexpansion

set FNLog=F:\xxcopy\29MAR2016 TEST Folder\Logs\Test1.log

set SrcDir=F:\xxcopy\NAS\FROM
set TgtDir=F:\xxcopy\NAS\TO
set FNClients=c:\ClientFolders.txt

for /d %%D in ("%SrcDir%\*") do (
  set ClientFullDir=%%D
  set ClientDir=%%~nD
  
  for /f %%a in (%FNClients%) do (
    if exist "!ClientFullDir!\%%a*" (
      for /d %%E in ("!ClientFullDir!\%%a*") do (
        echo xxcopy "!ClientFullDir!\%%~nE" "%TgtDir%\!ClientDir!\%%~nE" /SC /F /S /E /VL /YY /V /JV /RC /TTA0 /TCA /H /TCC /TCW /I /FW /oA:"%FNLog%" /oF3
        echo.
      )
    )
  )
)

Open in new window

Avatar of cmp119

ASKER

Well I tried running it, and nothing happens.  DOS window opens and then closes, log log file either.  Could be set clientdir=%%~nD, and the xxcopy refers to %%~nE?
Avatar of cmp119

ASKER

OOPs typos:  Well I tried running it, and nothing happens.  DOS window opens and then closes, no log file either.  Could it be "set clientdir=%%~nD", and the xxcopy command portion refers to "%%~nE"?
To clarify, for debugging reasons, all this version does is ECHO the results to the screen. If the results look like they will work for you, remove the ECHO to run it for real, as mentioned in last post.

Do this:
1. Open a CMD window
2. Either drag and drop the .bat file into the CMD window, or type out the full path and file name.
3. ENTER

The xxcopy commands will show for your visual verification.
Avatar of cmp119

ASKER

I completed the above steps of removing the @echo off, and dragged it to a command prompt, and it should everything fine, but does not do anything.  No log file is generated either.
Avatar of cmp119

ASKER

I reviewed your instructions once again, and removed "echo" on line 17 and tried running it, and it did not do anything either.  All I am doing is launching the batch file in windows.  It should run without issue.
It is not supposed to copy anything. Also, no log file should be made.

It should just show xxcopy lines in the CMD window. You are supposed to verify whether the folder names are correct.

Run it again in cmd window. Copy and paste (do not type) exact results.
Avatar of cmp119

ASKER

Okay, I was able to run it.  I modified the (%FNClients%) portion with double quotes ("%FNClients%")thinking that was the problem.  I removed the double quotes and it ran.  Will now start more extensive testing.  Sorry for the mix up.
Avatar of cmp119

ASKER

So I tried running it as suggested, and it echoed everything, but did not run.  I reviewed the logic and made small modifications by placing double quotes (%FNClients%), I also changed the path for the log file, FROM and TO location paths with no spaces, and also the path for the input file.  I then also removed "echo" from line 17 which was located just before the xxcopy command.  I also modified the input file using two different partial client folder names.  It ran it, and it moved the two client folders and its also reflected in the log file.  Below shows the adjustments made.


@echo off
setlocal enabledelayedexpansion

set FNLog=F:\xxcopy\29MAR2016TESTFolder\Logs\Test1.log

set SrcDir=F:\xxcopy\29MAR2016TESTFolder\NAS\FROM
set TgtDir=F:\xxcopy\29MAR2016TESTFolder\NAS\TO
set FNClients=F:\xxcopy\29MAR2016TESTFolder\ClientFolders.txt

for /d %%D in ("%SrcDir%\*") do (
  set ClientFullDir=%%D
  set ClientDir=%%~nD
 
  for /f %%a in (%FNClients%) do (
    if exist "!ClientFullDir!\%%a*" (
      for /d %%E in ("!ClientFullDir!\%%a*") do (
        xxcopy "!ClientFullDir!\%%~nE" "%TgtDir%\!ClientDir!\%%~nE" /SC /F /S /E /VL /YY /V /JV /RC /TTA0 /TCA /H /TCC /TCW /I /FW /oA:"%FNLog%" /oF3
        echo.
      )
    )
  )
)
>  It ran it, and it moved the two client folders and its also reflected in the log file.
I assume you're good to go.
Avatar of cmp119

ASKER

Originally there were spaces in the folder "29MAR2016 TEST Folder", so I placed double quotes before and after the whole path "F:\xxcopy\29MAR2016 TEST Folder\Logs\Test1.log", but it still did not run.  So I renamed this folder removing the spaces as "29MAR2016TESTFolder".  Once I am able to test everything using local test folders, I will need to run it so that that source and destination are located on a local NAS device.  I am hoping I can use UNCs for the source: \\NAS\ClientFolders\, and destination: \\NAS\MovedClientFolders\.
Avatar of cmp119

ASKER

Let me ask you several questions:

Referring to the below commands, if the path has spaces then should the whole path be enclosed in double quotes?

set FNLog=F:\xxcopy\29MAR2016TESTFolder\Logs\Test1.log
set SrcDir=F:\xxcopy\29MAR2016TESTFolder\NAS\FROM
set TgtDir=F:\xxcopy\29MAR2016TESTFolder\NAS\TO
set FNClients=F:\xxcopy\29MAR2016TESTFolder\ClientFolders.txt

I am trying to understand the logic in how you were able to get it use the input text file which only has the first six characters in the folder name and match it with client folder names that exist in specific subfolders?   What I am getting at is explanation of the command steps (logic) taken to derive the result.
ASKER CERTIFIED SOLUTION
Avatar of NVIT
NVIT
Flag of United States of America image

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
Avatar of cmp119

ASKER

NVIT is amazing!  His script works great!!!  Thanks a bunch!!!
You're welcome, cmp119! I'm glad to help.
Avatar of cmp119

ASKER

I just tried running this script on live data and now there are many problems.  Are you available to assist?
What errors, symptoms?
Avatar of cmp119

ASKER

I opened up another question titled "xxcopy batch file going wrong".  It's too much to write again, and it explains all the details of the problems encountered.  Basically when it encounters a top level client folder name with a period ".", that needs to be moved, it does not move it, and simply creates the folder structure for all the folders along side it at the destination with no files copied.  That's all wrong.  I also removes all empty folders from any client subfolder that is copied the subfolder structure.  You may want to review the other question mentioned above to get a better idea.