Solved

Batch file with spaces in file names

Posted on 2011-02-16
7
394 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
ID: 34908902
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
ID: 34908968
... 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
ID: 34909037
That was the key I was missing.  Worked great!  Thanks!
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 1

Author Comment

by:hlndadm
ID: 34909114
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
ID: 34909175
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
ID: 34909259
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
ID: 34909297
Perfect again!  Thanks for all your help.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Using dates in 'DOS' batch files has always been tricky as it has no built in ways of extracting date information.  There are many tricks using string manipulation to pull out parts of the %date% variable or output of the date /t command but these r…
One of my most closely kept secrets is revealed in this discussion How to output text on the same line This question was recently posted in EE by Simon336697 (http://www.experts-exchange.com/Programming/Languages/Scripting/Shell/Batch/Q_2459…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

895 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

11 Experts available now in Live!

Get 1:1 Help Now