Solved

Batch file with spaces in file names

Posted on 2011-02-16
7
418 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

TOMORROW TOMORROW.BAT is inspired by a question I get asked over and over again; that is, "How can I use batch file commands to obtain tomorrow's date?" The crux of this batch file revolves around the XCOPY command - a technique I discovered w…
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…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

733 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