Solved

Batch file with spaces in file names

Posted on 2011-02-16
7
378 Views
Last Modified: 2012-05-11
I am writing a batch file to search a directory for .xlsx files and create directories based off those file names.  I found the following online which works to a point.  Problem is my file name has spaces and I only get the first word of the file name as the directory name.

for /F %%a in ('dir /b "s:\bob and tom\*.xlsx"') do set FileName="%%~na"
mkdir "S:\bob and tom\%filename%"

Any idea how to make it read the spaces in the filename and put the same spaced format in the directory name?
0
Comment
Question by:hlndadm
  • 3
  • 3
7 Comments
 
LVL 33

Accepted Solution

by:
knightEknight earned 125 total points
Comment Utility
is this the entire script?  it appear that the filename variable is set in a loop, but the mkdir statement is after all the loops.

for /F "delims="  %%a in ('dir /b "s:\bob and tom\*.xlsx"') do mkdir "S:\bob and tom\%%~na"
0
 
LVL 33

Expert Comment

by:knightEknight
Comment Utility
... note the delims specifier above, and the use of %%~na in the mkdir -- these two together will keep the spaces in the directory names.
0
 
LVL 1

Author Closing Comment

by:hlndadm
Comment Utility
That was the key I was missing.  Worked great!  Thanks!
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 1

Author Comment

by:hlndadm
Comment Utility
Could I then copy the .xlsx file that it found to the directory it just created?  That is why I originally had the SET command.
0
 
LVL 33

Expert Comment

by:knightEknight
Comment Utility
for /F "delims="  %%a in ('dir /b "s:\bob and tom\*.xlsx"') do (
   mkdir "S:\bob and tom\%%~na" >nul
   copy   "S:\bob and tom\%%a"  "S:\bob and tom\%%~na"
)
0
 
LVL 95

Expert Comment

by:Lee W, MVP
Comment Utility
Another solution instead of delims is to use tokens -

for /f "tokens=*" ...

That says that the entire "line" should be one "entry" as for reads through it unstead of multiple ones.
0
 
LVL 1

Author Comment

by:hlndadm
Comment Utility
Perfect again!  Thanks for all your help.
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

The following is a collection of cases for strange behaviour when using advanced techniques in DOS batch files. You should have some basic experience in batch "programming", as I'm assuming some knowledge and not further explain the basics. For some…
Introduction: Recently, I got a requirement to zip all files individually with batch file script in Windows OS. I don't know much about scripting, but I searched Google and found a lot of examples and websites to complete my task. Finally, I was ab…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now