copy the first five digit to a matching folde

Microsoft Windows xp home edition with s...
May 14, 2010 at 07:11:01
Specs: Windows xp
I want to copy the first five digit of a files to a matching folder.

example

Files located c:\test\
11000_abc.dat
23600_abc.dat
35000_abc.dat

Folder located c:\test2\
folder name has follow:
110
236
350

Thank you.


See More: copy the first five digit to a matching folde

Report •


#1
May 14, 2010 at 11:27:14
Since you are trying to use all the characters before the '_' delimeter you should be able to use something like the following:


For /F "tokens=1 Delims=_" %%A in ('dir /b /a-d /s *.dat) Do (
IF not Exist C:\Test2\%%A MD C:\Test2\%%A
Copy /Y %%A*.dat C:\Test2\%%A
)

There is probably a better way to write it, but this should work.


Report •

#2
May 14, 2010 at 19:23:32
It didn't work. I get the following error message.

Token=1 delims-" was unexpected at this time.

the folder name is incorrect it should have 5 digit folder:

11000
23600
35000


Report •

#3
May 14, 2010 at 21:34:57
Kroc correctly had:
For /F "tokens=1 Delims=_" %%A in ('dir /b /a-d /s *.dat) Do (

apparently you mistakenly had:
For /F "tokens=1 Delims=-" %%A in ('dir /b /a-d /s *.dat) Do (

fix that and it should work.
Also, in your example you showed 3-digit folders. (?)
as usual, I'm confused.
Also might want to tweak it some depending:
For /F "tokens=1-2 Delims=_" %%A in ('dir /b /a-d /s *.dat) Do (
md %%A 2>nul
copy %%A_%%B.dat %%A
)

:: in case the wildcard gathers too many candidates.


Report •

Related Solutions

#4
May 15, 2010 at 08:28:32
It work with this but It would copy every files to this folder c:\test2\11000 only. I have variable folder the exist already like 11000, 23600, 35000 and the first five digit of a files matched with this folder and I want the files to copy itself to its matching folder. It's so close. I appreciate everyone helping me. I don't know if it can be done. Thank you.

For /F "tokens=1 Delims=_" %%A in ('dir /b /a-d /s *.dat') Do (
Copy /y %%A*.dat C:\test2\11000
)

I'm not sure but I think the reason is not working is because it's reading the whole file like 11000_abc.dat to match to folder 11000 and it's not matching up.

If I change the copy to: Copy /y %%A *.dat C:\test2\%%A the message I get is that it can't find the directory

Any idea what I'm missing to make it work?


Report •

#5
May 15, 2010 at 12:57:23
try this version:
For /F "tokens=1* Delims=_" %%A in ('dir /b /a-d /s *.dat) Do (
md %%A 2>nul
copy /y %%A_%%B c:\test\%%A
)
::--- end
be sure to pick up both tokens, as that is required in this
version (ie: tokens=1*, then use both %%A and %%B
in the filename at the copy-line.)
Here's the wildcard version if you prefer:
For /F "tokens=1 Delims=_" %%A in ('dir /b /a-d /s *.dat) Do (
md %%A 2>nul
copy /y %%A_*.dat c:\test\%%A
)

Report •

#6
May 16, 2010 at 08:45:43
I tried everyone's response. The last error I got from the last response is "a subdirectory or file name already exist. The file cannot be copied onto itself. This is really frustrating trying it to get it to work. Thank you for helping me out.

Report •

#7
May 17, 2010 at 05:57:10
I got it to work by messing around with it. What if the files you're working on is on another directory and you want those files copy to another directory and your batch file is outside from this directory how do I do that. Let say the files is in Test1 and you want those files copied into test2 folder and your batch program is in test3. This is all I need now. I tried I can't seem to get it working. Thank you.


For /F "tokens=1 Delims=_" %%A in ('dir /b /a-d /s *.pdf') Do (
cd\group
If exist c:\group\%%A copy /y %%A_*.pdf %%A\Monthly_Reports
)
:end
pause


Report •

#8
May 17, 2010 at 06:04:16
Sorry I gave you the wrong sample here it is.

For /F "tokens=1 Delims=_" %%A in ('dir /b /a-d /s *.dat') Do (
If exist %%A copy /y %%A_*.dat c:\test2\%%A
)
:end
pause

Report •

#9
May 17, 2010 at 16:12:36
you can either 1) navigate to the directory, or 2) operate from
wherever the batchfile is located.
method 1, just add this line before the for-loop begins:
pushd c:\test1
for /f ... ...

method 2, add the directory into the 'dir' command:
For /F "tokens=1 Delims=_" %%A in ('dir c:\test1 /b /a-d /s *.dat') Do (
:: recommend this line add
if not exist c:\test2\%%A md c:\test2\%%A
copy /y %%A_*.dat c:\test2\%%A
)


Report •

#10
May 21, 2010 at 06:09:42
I did exactly what you said to put and I come up with this error
Filename, Directory name or volume labe name syntax is incorrect:
pushd c:\test1
For /F "tokens=1 Delims=_" %%A in ('dir /b /a-d /s *.dat') Do (
If not exist c:\test2\%%A md c:\test2\%%A
copy /y %%A_*.dat c:\test2\%%A
)
:end
pause

Report •

#11
May 27, 2010 at 06:16:35
The files is in test1 folder and the folder I want the files to be copied on is in test2. The batch file itself is in c: root directory.

The path to the files or folder have no space therefore it does not required equote

If I put the files and the folder and the batch file in the same directory it seems to be working fine.



Report •

Ask Question