?
Solved

Batch file with spaces in file names

Posted on 2011-02-16
7
Medium Priority
?
447 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 500 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
Veeam Disaster Recovery in Microsoft Azure

Veeam PN for Microsoft Azure is a FREE solution designed to simplify and automate the setup of a DR site in Microsoft Azure using lightweight software-defined networking. It reduces the complexity of VPN deployments and is designed for businesses of ALL sizes.

 
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 96

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

Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

Question has a verified solution.

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

VALIDATING DATES One method of validating dates is to jam the date into the DATE command and see if it accepts it by examining the system's errorlevel value. A non-zero result indicates failure. A typical example might look something like the fol…
YESTERDAY YESTERDAY.BAT is inspired by a previous article I wrote entitled: TOMORROW.BAT (http://www.experts-exchange.com/OS/Microsoft_Operating_Systems/MS_DOS/A_4196-Advanced-Batch-File-Programming-TOMORROW-BAT.html). The crux of this batch f…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Suggested Courses

770 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