Solved Batch file to compare names and move data using spreadsheet

Microsoft Office professional 2010 softw...
June 11, 2014 at 17:42:59
Specs: Windows 2008, 8G
Hi

I have a batch file that will compare D:\users\classes sub-folder with D:\users\children sub-folder's and if the name matches it will move all the sub-folders and files over from children to classes. eg. d:\users\children\1b\aidan.chan and d:\users\classes\1b\aidan.chan. all sub-folders and files from the children aidan.chan will move to classes aidian.chan.


@echo off
setlocal
if exist test del test
for /f "tokens=4,5 delims=\" %%a in ('dir /b /s /ad d:\users\classes') do if not "%%b" == "" (
for /f "tokens=4,5 delims=\" %%m in ('dir /b /s /ad d:\users\children') do if not "%%n" == "" (
if /i %%b equ %%n (
>>test echo d:\users\children\%%m\%%n will be copied to: d:\users\classes\%%a\%%b. All subdirectories incl.
>>test robocopy /MOV /S /R:1 d:\users\children\%%m\%%n d:\users\classes\%%a\%%b
)
)
)


But now the folder name of all these children will change and I will have a spreadsheet with their new folders name. eg. adian.chan folder will be 16chan10.

I will need to move all his data and sub-folders from D:\users\classes\1b\adian.chan over to D:\users\class2014\1b\16chan10

Can anyone help me to modify the batch file to do this please, much appreciated since I am not a programmer. Thanks in advance.



See More: Batch file to compare names and move data using spreadsheet

Report •

✔ Best Answer
June 12, 2014 at 23:07:25
Ok. from your sample, this is the only line that needs changed FROM:
for /f "tokens=1,2" %%a in (assoc) do set %%a=%%b
TO:
for /f "tokens=2,4" %%a in (assoc) do set %%a=%%b
since the "midlevel" directory, tokens 1,3 ("1b", "1k") is not relevant to this problem.

Effectively, you should have a variable: %aidan.chan%
which has a value of: chana37

The spread-sheet export, as thus far addressed, should not be comma delim, but space/tab delim (either one, don't matter). But batch can be rewritten to use CSV instead if that works better.



#1
June 11, 2014 at 19:02:59
Any text association-table is fairly simple for batch, but spreadsheets are not an option for batch. You must either manually "textify" the spreadsheet, or, if the spreadsheet has the equipment, use a higher-level script to access it directly (f/e: Excel exposes methods and vars to do this). The association. table is straightforward, but highly dependant on the format. F/e, simplest, two items per line, item one is child, item two is class, delimited with space:
for /f "tokens=1,2" %%a in (assoc) do (
if /i "%child%" equ "%%a" set class=%%b&goto :eof
)
So I guess the spreadsheet thing is the question: how you will extract it to a text table, and what format the table will be.

Report •

#2
June 11, 2014 at 22:24:37
Is it possible to have it in 1 cell existing name and another cell the new name. I can save it as txt or whatever.

Then change the script if the cell 1 name match cell 2 name then move the files over.

I will have their new names in the class2014\1b folder etc already created and it will be blank. Just run the script and copy the files and sub folders over?



Report •

#3
June 12, 2014 at 19:07:27
The best thing would be if you could post a sample of the text-saved spreadsheet, but I'll assume two tokens per line as I described in resp#1, for this trial:

@echo off & setlocal enabledelayedexpansion
:: load the associations table: 1st item is class-name, second is child-name
for /f "tokens=1,2" %%a in (assoc) do set %%a=%%b
del test 2>nul
for /f "tokens=4,5 delims=\" %%a in ('dir /b /s /ad d:\users\classes') do if not "%%b" == "" (
for /f "tokens=4,5 delims=\" %%m in ('dir /b /s /ad d:\users\children') do if not "%%n" == "" (
if /i !%%b! equ %%n (
>>test echo d:\users\children\%%m\%%n will be copied to: d:\users\classes\%%a\!%%b!. All subdirectories incl.
>>test robocopy /MOV /S /R:1 d:\users\children\%%m\%%n d:\users\classes\%%a\!%%b!
)
)
)
::======== end batch, not tested!
It was too much like work to build the test structure, and also too many unknowns to invest the time. I'll wait for feedback.

message edited by nbrane


Report •

Related Solutions

#4
June 12, 2014 at 21:21:13
class, existing folder name, new class new folder name

1b aidan.chan 2k chana37
1b lora.smith 2l smithl45
1b mark.mansfield 2k mansm23
1k ryo.chan 2l chanr30
1k curti.jan 2k janc56
1k annise.famer 2l famera32
etc

I can put all the data into spreadsheet and save it as CSV, or txt (tab delimited) or whatever format it will work in.

The existing folder names are already being used d:\users\classes\1b\aidan.chan\ , D:\users\classes\1b\lora.smith\ ......

I will create all the new folders in D:\users\children\2k\chana37\, D:\users\children\2l\smithl45.........


So basically just need to run the batch file, look up the csv or txt file and move all folders and data from aidan.chan to chana37 etc...




Report •

#5
June 12, 2014 at 23:07:25
✔ Best Answer
Ok. from your sample, this is the only line that needs changed FROM:
for /f "tokens=1,2" %%a in (assoc) do set %%a=%%b
TO:
for /f "tokens=2,4" %%a in (assoc) do set %%a=%%b
since the "midlevel" directory, tokens 1,3 ("1b", "1k") is not relevant to this problem.

Effectively, you should have a variable: %aidan.chan%
which has a value of: chana37

The spread-sheet export, as thus far addressed, should not be comma delim, but space/tab delim (either one, don't matter). But batch can be rewritten to use CSV instead if that works better.


Report •

Ask Question