Jump to content

Welcome to MSFN Forum
Register now to gain access to all of our features. Once registered and logged in, you will be able to create topics, post replies to existing threads, give reputation to your fellow members, get your own private messenger, post status updates, manage your profile and so much more. This message will be removed once you have signed in.
Login to Account Create an Account



Photo

handling special characters in folder/file names

- - - - -

  • Please log in to reply
14 replies to this topic

#1
radix

radix

    Friend of MSFN

  • Member
  • PipPipPipPipPip
  • 743 posts
  • Joined 08-February 07
  • OS:Windows 8 x64
  • Country: Country Flag

Hello,

I got this folder structure:

"\$1\Install\CCleaner_x86_&_x64"

 

In $1 I have Install.cmd which call a cmd file located in the above folder structure: "\$1\Install\CCleaner_x86_&_x64\CCleaner_x86_&_x64.cmd"

CCleaner_x86_&_x64.cmd will install CCleaner and it's OK.

 

Install.cmd from $1 folder have this content:

@Echo Off
:: Install CCleaner
START CMD /C "CD %~dp0Install\Piriform_CCleaner_5.1.5075_x86_&_x64 & CCleaner_x86_&_x64.cmd"

 

and don't work. If I remove & characters from folder and file names, it will work fine.

So, it's possible to succesfully call the second cmd file if it's located in a folder structure that contain & character and/or spaces?

Thanks


Edited by radix, 21 January 2015 - 01:41 PM.



How to remove advertisement from MSFN

#2
Yzöwl

Yzöwl

    Wise Owl

  • Super Moderator
  • 4,053 posts
  • Joined 13-October 04
  • OS:Windows 7 x64
  • Country: Country Flag

Donator

Remove the & characters then!

 

The folder/file names are your choice so choose something sensible for the environment and scripting language you're using.



#3
radix

radix

    Friend of MSFN

  • Member
  • PipPipPipPipPip
  • 743 posts
  • Joined 08-February 07
  • OS:Windows 8 x64
  • Country: Country Flag

So, these are the limit of batch scripting. Thanks!



#4
gunsmokingman

gunsmokingman

    MSFN Master

  • Super Moderator
  • 2,225 posts
  • Joined 02-August 03
  • OS:none specified
  • Country: Country Flag
Perhaps a VBS script might work, place the below script in the folder and it should open Test.txt if it there.
Attached File  SpecailCharaterVBS.png   59.39KB   3 downloads
Dim Fso :Set Fso = CreateObject("Scripting.FileSystemObject")
 If Fso.FileExists("Test.txt") Then
  WScript.Echo "Confirm : " & _
  Replace(WScript.ScriptFullName,WScript.ScriptName,"") & "Test.txt"
  CreateObject("Wscript.Shell").Run("Test.txt"),1,False
 Else
  WScript.Echo "Missing : " & _
  Replace(WScript.ScriptFullName,WScript.ScriptName,"") & "Test.txt"
 End If 
Contents Of Test.txt
Testing $ in pathway.
  • radix likes this


GunSmokingMan



#5
jaclaz

jaclaz

    The Finder

  • Developer
  • 15,189 posts
  • Joined 23-July 04
  • OS:none specified
  • Country: Country Flag

It is not about "limits" of batch files.

 

The concept of "special" characters is that a few characters are "special" ;).

 

Which while it remains true :yes: that it makes very little sense (actually NO sense :no:) to use a "special" character in a file name as it is the perfect way to make your own batches/scripts/whatever more complex than needed and have *any* third party batch/script/whatever fail, the "special" character can be dealt with - in a "special" way ;).

Namely you need to escape "special" characters:

http://www.robvander...escapechars.php

 

@gunsmokingman

"$" is not AFAIK/AFAICR a "special" character in batch. :w00t:

 

jaclaz


  • radix likes this

#6
radix

radix

    Friend of MSFN

  • Member
  • PipPipPipPipPip
  • 743 posts
  • Joined 08-February 07
  • OS:Windows 8 x64
  • Country: Country Flag

Thanks jaclaz to point in the right direction.

The working batch script:

START /WAIT CMD /C "CD %~dp0Install\Piriform_CCleaner_5.1.5075_x86_^&_x64 & Piriform_CCleaner_5.1.5075_x86_^&_x64.cmd"

Edited by radix, 22 January 2015 - 10:30 AM.


#7
Yzöwl

Yzöwl

    Wise Owl

  • Super Moderator
  • 4,053 posts
  • Joined 13-October 04
  • OS:Windows 7 x64
  • Country: Country Flag

Donator

You may in your example above, be better advised using START "" /WAIT, where "" is "EMPTY TITLE".

 

 

It's still a pointless exercise to have a file and folder structure over which you have full control and deliberately use naming structures which are not suitable.

 

The folder inside $1 you have named Install, all of the sub folders and their contained batch files can be named anything at all, so why choose something which means that you need to take special action within their contained installation scripts.

 

Take for instance the example above, everyone knows that the program is called CCleaner, nobody, except for those directly involved with the Developer cares at installation time who makes it so drop the Piriform_ from both the directory and file names. If I was to only have x64 version of any software then I would include x64 in the names otherwise I know that the software would install on either platform. So since this obviously installs on both platforms, just drop the _x86_&_x64 from the names. Additionally, the installation batch file in every folder doesn't need to carry the same name as the parent folder, if it's in the CCleaner directory it's relatively safe to assume it isn't used to install Opera in the Opera directory, so just tell us what it does by giving it a sensible name. Then you should be creating the individual batch files to use their parent directories if necessary for the current directories in order that you don't have to specifically code it in your working batch script.

 

Examples:

CALL "%~dp0Install\CCLeaner_5.1.5075\install.cmd"

START "" /W CMD /C "%~dp0Install\CCLeaner_5.1.5075\install.cmd"

 

The system above also means that if you wanted to install the following five programs you could use something like this:

FOR %%A IN ("Prog_1" "Prog_2" "Prog_3" "Prog_4" "Prog_5") DO CALL "%~dp0Install\%%~A\install.cmd"


#8
gunsmokingman

gunsmokingman

    MSFN Master

  • Super Moderator
  • 2,225 posts
  • Joined 02-August 03
  • OS:none specified
  • Country: Country Flag
Well Jaclas my script will run with special charaters in the name.

Attached Files


  • radix likes this


GunSmokingMan



#9
jaclaz

jaclaz

    The Finder

  • Developer
  • 15,189 posts
  • Joined 23-July 04
  • OS:none specified
  • Country: Country Flag

Well Jaclas my script will run with special charaters in the name.

I never doubted it :), I am pretty sure that your .vbs (or other) scripts are well thought out and well written :thumbup , I was only pointing out how your earlier example:

Testing $ in pathway.

was not a valid example of a test of *anything* for a "special" character in a path (because "$" is not "special").

 

Gotta go :hello: , I have to test my new scuba diving equipment and - strangely enough - I go underwater to test it, as last time I tested one by making a short (but brisk ;)) walk outside it came out eventually that it was not fully waterproof...

 

@radix

Well, you have been told (and re-told):

http://homepage.ntlw...oolishness.html

 

jaclaz



#10
bphlpt

bphlpt

    MSFN Addict

  • Member
  • PipPipPipPipPipPipPip
  • 1,915 posts
  • Joined 12-May 07
  • OS:none specified
  • Country: Country Flag

@jaclaz, I believe you have an extra period "." in your link above after "com" - ntlworld.com./jonathan.  Without it, the link works fine. - http://homepage.ntlw...oolishness.html.

 

Cheers and Regards


Posted Image


#11
jaclaz

jaclaz

    The Finder

  • Developer
  • 15,189 posts
  • Joined 23-July 04
  • OS:none specified
  • Country: Country Flag

@jaclaz, I believe you have an extra period "." in your link above after "com" - ntlworld.com./jonathan.  Without it, the link works fine. - http://homepage.ntlw...oolishness.html.

 

Cheers and Regards

Yes/no :w00t: :ph34r:

 

Meaning that yes :yes:, there is actually an "extra dot after the com" in the posted link :blushing:, which is a (recent) copy/paste from the generic DeBoyne Pollard's FGA's page:

http://homepage.ntlw...ynepollard/FGA/

but no :no:, the link works fine here with or without the extra dot (... like we were talking here of special characters....)

I wonder HOW I managed to insert it :unsure:

 

jaclaz



#12
radix

radix

    Friend of MSFN

  • Member
  • PipPipPipPipPip
  • 743 posts
  • Joined 08-February 07
  • OS:Windows 8 x64
  • Country: Country Flag

Hello,

I got a similar problem with the next batch script, trying to replace a string with another one in multiple files:

@ECHO OFF
SETLOCAL

for %%* in (.) do set new=%%~n*
SET "new=news tring"
SET "old=XX XX"
for %%f in (*.txt) do (
    echo Processing %%f...
    (
    FOR /F "delims=" %%l IN (%%f) DO (
        SET "line=%%l"
        SETLOCAL ENABLEDELAYEDEXPANSION
        set "x=!line:%old%=%new%!"
        ECHO(!x!
    ENDLOCAL
    )
    )>%%~nf.new
)
::for %%f in (*.new) do move /y "%%f" "%%~nf.txt"
GOTO :EOF

Files that contain spaces in their names will not be edited.

I tried with these lines of code:

FOR /F "delims=" %%l IN (%%f) DO (
FOR /F "delims=" %%l IN (^"%%f^") DO (

as it's explained here: http://ss64.com/nt/syntax-dequote.html

but didn't work to escape ""



#13
MHz

MHz

    Just simple

  • Member
  • PipPipPipPipPipPipPip
  • 1,667 posts
  • Joined 02-August 04
  • OS:Windows 7 x64
  • Country: Country Flag

Hello radix,
 
IMO, best place to look at first is the command For /?. Look for the usebackq option.
 
Quote from For /?:

usebackq        - specifies that the new semantics are in force,
                  where a back quoted string is executed as a
                  command and a single quoted string is a
                  literal string command and allows the use of
                  double quotes to quote file names in
                  file-set.

 
To change behaviour of how quoting works, use the backquotes option.
 
Using the backquotes option allows you to use double quotes to brace the variable that stores the path that may also have some whitespace.

FOR /F "usebackq delims=" %%l IN ("%%f") DO (

  • radix likes this

#14
radix

radix

    Friend of MSFN

  • Member
  • PipPipPipPipPip
  • 743 posts
  • Joined 08-February 07
  • OS:Windows 8 x64
  • Country: Country Flag

 

To change behaviour of how quoting works, use the backquotes option.

 
Using the backquotes option allows you to use double quotes to brace the variable that stores the path that may also have some whitespace.

FOR /F "usebackq delims=" %%l IN ("%%f") DO (

 

Hello,

It's not working, runing the test in a virtual machine generate file content like this one:

ALLUSERSPROFILE=C:\Documents and Settings\All Users
APPDATA=C:\Documents and Settings\Power User\Application Data
CLIENTNAME=Console
CommonProgramFiles=C:\Program Files\Common Files
COMPUTERNAME=POWERUSER-PC
ComSpec=C:\WINDOWS\system32\cmd.exe
DEVMGR_SHOW_DETAILS=
DEVMGR_SHOW_NONPRESENT_DEVICES=1
FP_NO_HOST_CHECK=NO
HOMEDRIVE=C:
HOMEPATH=\Documents and Settings\Power User
LOGONSERVER=\\POWERUSER-PC
new=news tring
NUMBER_OF_PROCESSORS=1
old=XX XX
OS=Windows_NT
Path=C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem
PATHEXT=.COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH
PROCESSOR_ARCHITECTURE=x86
PROCESSOR_IDENTIFIER=x86 Family 6 Model 58 Stepping 9, GenuineIntel
PROCESSOR_LEVEL=6
PROCESSOR_REVISION=3a09
ProgramFiles=C:\Program Files
PROMPT=$P$G
SESSIONNAME=Console
SystemDrive=C:
SystemRoot=C:\WINDOWS
TEMP=C:\DOCUME~1\POWERU~1\LOCALS~1\Temp
TMP=C:\DOCUME~1\POWERU~1\LOCALS~1\Temp
USERDOMAIN=POWERUSER-PC
USERNAME=Power User
USERPROFILE=C:\Documents and Settings\Power User
windir=C:\WINDOWS
gdfgdfgdfnews tringgdfhfghfg

where gdfgdfgdfnews tringgdfhfghfg is the file name.


Edited by radix, Today, 04:05 AM.


#15
jaclaz

jaclaz

    The Finder

  • Developer
  • 15,189 posts
  • Joined 23-July 04
  • OS:none specified
  • Country: Country Flag

I am not understanding what/why you want to dequote.

The issue here is more like wrapping filenames with spaces into double quotes.

 

I would use something *like*:

@ECHO OFF
SETLOCAL ENABLEEXTENSIONS ENABLEDELAYEDEXPANSION
SET New_string=new
SET Old_string=old
FOR /F "tokens=* delims=" %%A IN ('DIR /B *.txt') DO echo Processing %%A...&CALL :do_replace "%%A"
GOTO :EOF

:do_replace
IF EXIST "%~nx1.new" DEL "%~nx1.new"
SET tomod=0
FOR /F "tokens=* delims=" %%B IN ('TYPE "%~nx1"^|FIND "%Old_string%"') DO SET tomod=1
IF %tomod%==0 ECHO No match...&ECHO.&GOTO :EOF

FOR /F "tokens=* delims=" %%B IN ('TYPE "%~nx1"') DO (
SET Line=%%B
SET ModLine=!Line:%Old_string%=%New_string%!
IF NOT "!Line!"=="!Modline!" ECHO !Line!&ECHO !Modline!&ECHO.&&SET tomod=1
ECHO !Modline!>>"%~nx1.new"
)
GOTO :EOF

But actually it makes little sense, exactly because a line in a .txt file may contain any kind of special character, and there are handy and faster solution.

 

My advice is to use gsar.exe and call it a day, compare with this thread where the parsing of "plain text file" has been used (and abused):

http://www.msfn.org/...two-text-files/

 

jaclaz






0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users