China DOS Union

-- Unite DOS · Advance DOS · Grow DOS --

Union site: www.cn-dos.net Forum site: www.cn-dos.net/forum
DOS stands for freedom, openness and progress. Let us work hard, learn from the openness and GNU spirit of FreeDOS and Linux, and together build and grow a free GNU GPL world!

中国DOS联盟论坛
The time now is 2026-08-06 04:26
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » How to create same-named folders for same-named files and copy them into the same-named folders? Solved View 1,126 Replies 10
Original Poster Posted 2007-10-16 20:16 ·  中国 江苏 苏州 电信
初级用户
★★
Credits 113
Posts 51
Joined 2007-02-15 22:54
19-year member
UID 79590
Gender Male
Status Offline
Good evening, friends. I have a question for you:
For example, in a folder on drive C there are A-001.ppt, A-002.ppt, A-004.PPT....*.ppt
How can I use a batch file to automatically create folders in the current directory named A-001, A-002, A-004....* and so on, matching the filenames, and then automatically copy A-001.ppt, A-002.ppt, A-004.PPT....*.ppt into the same-named folders? (For example: the newly created A-001 folder contains A-001.ppt)

Thank you all for your replies!

[ Last edited by superadam on 2007-10-16 at 09:29 PM ]
Floor 2 Posted 2007-10-16 20:18 ·  中国 江苏 苏州 电信
初级用户
★★
Credits 113
Posts 51
Joined 2007-02-15 22:54
19-year member
UID 79590
Gender Male
Status Offline
I just saw that moderator lxmxn is online, please help, I would be truly grateful!

[ Last edited by superadam on 2007-10-16 at 08:21 PM ]
Floor 3 Posted 2007-10-16 20:41 ·  中国 江苏 苏州 电信
初级用户
★★
Credits 113
Posts 51
Joined 2007-02-15 22:54
19-year member
UID 79590
Gender Male
Status Offline
All you DOS veterans, please help, I need it urgently, thank you.
Floor 4 Posted 2007-10-16 20:43 ·  中国 湖北 武汉 电信
版主
★★★★★
Credits 11,386
Posts 4,938
Joined 2006-07-23 17:10
20-year member
UID 59080
Status Offline
Try this:

@echo off
for %%a in (C:\YourDir\*.ppt) do (
md "%%~dpna" && copy "%%a" "%%~dpna"
)
Floor 5 Posted 2007-10-16 20:59 ·  中国 江苏 苏州 电信
初级用户
★★
Credits 113
Posts 51
Joined 2007-02-15 22:54
19-year member
UID 79590
Gender Male
Status Offline
First, thanks to moderator lxmxn. I'll try it first. Many thanks. Come visit my home sometime when you're free, hehe.
Floor 6 Posted 2007-10-16 21:04 ·  中国 江苏 苏州 电信
初级用户
★★
Credits 113
Posts 51
Joined 2007-02-15 22:54
19-year member
UID 79590
Gender Male
Status Offline
Originally posted by lxmxn at 2007-10-16 08:43 PM:
Try this:

@echo off
for %%a in (C:\YourDir\*.ppt) do (
md "%%~dpna" && copy "%%a" "%%~dpna"
)



Amazing, thank you so much, it really works. Looks like I made the problem more complicated than it was. Thank you very, very much!
Floor 7 Posted 2007-10-16 21:08 ·  中国 江苏 苏州 电信
初级用户
★★
Credits 113
Posts 51
Joined 2007-02-15 22:54
19-year member
UID 79590
Gender Male
Status Offline
By the way, one more question: how should %%~dpna be explained? Please enlighten me, thanks!
It looks to me like a naming path, but I don't quite understand it. Please explain :)

[ Last edited by superadam on 2007-10-16 at 09:11 PM ]
Floor 8 Posted 2007-10-16 21:12 ·  中国 湖北 武汉 电信
版主
★★★★★
Credits 11,386
Posts 4,938
Joined 2006-07-23 17:10
20-year member
UID 59080
Status Offline
Hehe, you're too polite.

Look at the last page of for help.
Floor 9 Posted 2007-10-16 21:15 ·  中国 江苏 苏州 电信
初级用户
★★
Credits 113
Posts 51
Joined 2007-02-15 22:54
19-year member
UID 79590
Gender Male
Status Offline
I'm not just being polite. I was rushing to prepare materials at work, and when I urgently needed this, you stepped in, so of course I'm extremely grateful. A timely help indeed.
Is it this: %~dpI - Expands %I to a drive letter and path only

[ Last edited by superadam on 2007-10-16 at 09:17 PM ]
Floor 10 Posted 2007-10-16 21:23 ·  中国 湖北 武汉 电信
版主
★★★★★
Credits 11,386
Posts 4,938
Joined 2006-07-23 17:10
20-year member
UID 59080
Status Offline
Save this here as a batch file and run it:
@echo off
echo The value of %%~f0 is:
echo Full path of the file:%~f0
echo The value of %%~s0 is:
echo Short path of the file:%~s0
echo The value of %%~z0 is:
echo File size:%~z0
echo There are many others like these, refer to the for command!
pause
Floor 11 Posted 2007-10-16 21:25 ·  中国 江苏 苏州 电信
初级用户
★★
Credits 113
Posts 51
Joined 2007-02-15 22:54
19-year member
UID 79590
Gender Male
Status Offline
Originally posted by lxmxn at 2007-10-16 09:23 PM:
Save this here as a batch file and run it:
@echo off
echo The value of %%~f0 is:
echo Full path of the file:%~f0
echo The value of %%~s0 is:
echo Short path of the file:%~s0
echo The value of %%~z0 is ...



Okay, thank you very much. I've saved it, and I'll take a slow look at it tomorrow, :lol: thanks. I'm heading back now, you should get some rest early too. Good night. Thanks again!
Forum Jump: