Solved

Batch file with spaces in file names

Posted on 2011-02-16
7
402 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
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 
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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

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…
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…
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

786 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