You are not logged in.

#1 02 May 2007 21:01

mrblitz
Member
From: Germany
Registered: 02 May 2007
Posts: 3

DIR a path with spaces

hi masters of scripting,

first at all, im a batch-newbie... so, dont hurt me if i ask dump questions ;-)

im sitting at my first-ever-self-scripted-monster-script, that allows a user to move files from a given folder to another given folder, which are stored in a config-file and creating a log with moved files.
the plan is to automate this via taskplaner later.

here is the thing:

@echo on

:: ask if config.ini is allready in the house
:start
IF EXIST config.ini (
GOTO rdcfg
) ELSE (
GOTO install
)

:: creating config.ini
:install
ECHO please enter source-path
ECHO.
SET /p source_inst=
ECHO.
ECHO please enter destination-path
ECHO.
SET /p dest_inst=
ECHO.
ECHO %source_inst%;%dest_inst% > config.ini

:: creating log.txt
ECHO what has been moved when, log createt on %date% at %time% > log.txt
ECHO. >> log.txt
GOTO start

:: getting pathnames from config.ini
:rdcfg
FOR /f "tokens=1 delims=;" %%a IN (config.ini) DO SET source=%%a
FOR /f "tokens=2 delims=;" %%a IN (config.ini) DO SET dest=%%a
ECHO %source%
ECHO %dest%
GOTO move

:: moving files...
:move

:: writing date and time in log.txt
ECHO ----------- >> log.txt
ECHO %date% %time% >> log.txt

:: reading source-dir, writing into temp dir-file
DIR /O:G /B %source% > dir.txt
:: proving if files are present
FOR /f "skip=1" %%a in (dir.txt) do set n=%%a
:: moving files
IF DEFINED n (
MOVE %source%\*.* %dest% >> log.txt
) ELSE (
echo No work do to... >> log.txt
)

:: cleaning up the mess
DEL dir.txt
SET a=
SET n=
SET source=
SET dest=
SET source_inst=
SET dest_inst=
:eof

my problem is now, that if the user is entering pathnames with spaces in, the script will crash, caused by failing the DIR command.

is there a workaround for this problem? (hope so ;-))

thanks for any comments and help

greetz
mrblitz

Offline

#2 03 May 2007 14:22

bluesxman
Member
From: UK
Registered: 29 Dec 2006
Posts: 1,129

Re: DIR a path with spaces

How about this?

DIR /O:G /B "%source%" > dir.txt

You'll also need to fix this too:

MOVE "%source%\*.*" "%dest:~0,-1%" >> log.txt

Besides that, you could tighten up the code a bit, such that this:

FOR /f "tokens=1 delims=;" %%a IN (config.ini) DO SET source=%%a
FOR /f "tokens=2 delims=;" %%a IN (config.ini) DO SET dest=%%a

Becomes this:

FOR /f "tokens=1,2 delims=;" %%a IN (config.ini) DO SET source=%%a&SET dest=%%b

cmd | *sh | ruby | chef

Offline

#3 03 May 2007 20:17

mrblitz
Member
From: Germany
Registered: 02 May 2007
Posts: 3

Re: DIR a path with spaces

hi bluesxman,
the quotes fixed it, thanks. sometimes small things will change the world... tongue


but im not shure for what

:~0,-1%

in

MOVE "%source%\*.*" "%dest:~0,-1%" >> log.txt

stands for?

cause its working also without it roll

thanks four your help

mrblitz

Offline

#4 04 May 2007 09:31

bluesxman
Member
From: UK
Registered: 29 Dec 2006
Posts: 1,129

Re: DIR a path with spaces

The ":~0,-1" bit is one of a few ways of getting around one of the little foibles in the way "cmd" scripts work.
The way you're writing your data to the config.ini file ...

ECHO %source_inst%;%dest_inst% > config.ini

... will cause a space character to appear at the end of the line (have a look at config.ini in notepad, you'll see what I mean) and thus the %dest% variable will also be appended with this extraneous character when you do the move command, rendering it useless.

This would've been transparent before because you weren't encapsulating the %dest% variable in quotes.  Now that you (presumably) are using quotes on the source and destination in the move command, it will break it as your target directory doesn't exist.  The ":~0,-1" bit is stripping the last character off the %dest% variable (IE the space) at run time.


cmd | *sh | ruby | chef

Offline

#5 04 May 2007 12:07

mrblitz
Member
From: Germany
Registered: 02 May 2007
Posts: 3

Re: DIR a path with spaces

hi,

thanks for the explanation and your help... smile

mrblitz

Offline

Board footer

Powered by