Im trying to run the following code to go through a directory and modify a bunch of autoCAD drawings.
FOR %%f in (C:\Users\pzf6bm\Desktop\AutoCad\*.dwg) do start /wait C:\"Program Files"\"Autodesk"\"AutoCAD 2012 - English"\acad.exe “%%f” /b C:\Users\pzf6bm\Desktop\AutoCad\hide.scr
It almost works except for one thing, everytime it retrieves the next filepath it adds an ô character to the beginning of the string and a ö to the end of the string. This makes autoCAD not able to open the file to perform the action.
As an example "C:\test.txt" becomes "ôC:\test.txtö" I have no idea why this happens.
just use right double quotes, not “%%f”!
"%%f"
You have smart-quotes around your %%f (perhaps you got this from Outlook or Word?). Retype them as regular quotes and your ô and ö will be gone.
EDIT: Normally the start command needs a blank set of quotes after it, because the first set of quotes is used as the window title. In your case the fact that c:\ is an unquoted part protects the quoted portions. Thanks to #dbenham for pointing that out.
Your command (after the start "" ) has quotes in an awkward mix, where they should normally be placed at the beginning and end of the "c:\path\filename.exe" so see my answer below, with a leading set of blank quotes too.
With modern tools even a short path and filename can be quoted, so it is easy to remember - just quote all terms.
FOR %%f in (C:\Users\pzf6bm\Desktop\AutoCad\*.dwg) do start "" /wait "C:\Program Files\Autodesk\AutoCAD 2012 - English\acad.exe" "%%f" /b "C:\Users\pzf6bm\Desktop\AutoCad\hide.scr"
Related
ok simple i want to make a batch script that:
Makes export folder on users desktop
from new (as in not copying from somewhere) make a batch script in that folder that lists the contents of that folder after the user populates it and saves it to the desktop.
the problem i run in to is I'm trying to use echo to to copy the intended new script text from the original batch file into the new one like this:
#echo off
mkdir "C:\Users\%username%\Desktop\Export"
echo dir "C:\Users\%username%\Desktop\Export" /W /A:-H /B > "C:\Users\%username%\Desktop\Readout.txt" > "C:\Users\%username%\Desktop\Export\Directoty_List.bat"
the problem is that the echo command sees the ">" as the end of the statement and writes the first part to a desktop text file, but i want it to see the 2nd ">" as that. how do i work around this?
Thank you
As #Richard said in the comments, you can escape the > with a carret sign ^>. Most characters with special meaning in cmd can be escaped with the carret if you don't want to use that special meaning.
Normally the golden rule is: put everything between double quotes. Inside double quotes, the characters with special meanings are also escaped. Unfortunately the echo will print the quotation marks too so this won't really help in your case. It still is worth mentioning though, quotation marks help when setting values of variables with special characters, passing arguments with special characters, define paths with special characters (whitespaces or parenthesis for eg.), ... but not with echo.
There is one more advice I'd like to give: you can use %userprofile% instead of specifying the whole path C:\Users\%username%
#echo off
mkdir "%userprofile%\Desktop\Export"
echo dir "%userprofile%\Desktop\Export" /W /A:-H /B ^> "%userprofile%\Desktop\Readout.txt" > "%userprofile%\Desktop\Export\Directoty_List.bat"
I am trying to open a file using a program. I would like to automatically do this with a batch file. Everything I need to run is in the same folder. I am not that good at batch files or coding but I am trying to get better, if anyone knows how to do this that would be great.
I have tried
.\location\application .\location\file
exit
and
START /B /I ".\location\application" ".\location\file"
exit
i have tried switching the order of the app and file but it does not work, most of the time it does nothing, but sometimes it opens the app but does not open the file.
The approach:
START /B /I ".\location\application" ".\location\file"
will fail because Start takes the first argument in parentheses double quotes as the window title. Insert a dummy pair of double quotes to circumvent this.
START "" /B /I ".\location\application" ".\location\file"
The help start /? doesn't explicitly state this.
ss64.com only says:
Always include a TITLE this can be a simple string like "My Script" or
just a pair of empty quotes "" According to the Microsoft
documentation, the title is optional, but depending on the other
options chosen you can have problems if it is omitted.
I am using robocopy to copy from one directory to another.
I have tested the code using miscellaneous source directories but I get an error when executing one particular directory:
set "source=C:\Program Files (x86)\Phoresis\Backup"
I get the error:
Invalid paramter 3 "(x86)\Phoresis\Backup"
Have tried using single quotes and have wrapped the variable assignments in "" as there are spaces in the directory paths.
Code:
set "newDirectory=F:\Cap2 Flex Backup"
REM Timesatmp a new folder
set "DirName=%date:~-4,4%.%date:~-7,2%.%date:~0,2%.%time:~0,2%.%time:~3,2%
MD \%newDirectory%"
set "source=C:\Program Files (x86)\Phoresis\Backup"
robocopy %source% %DirName% /e /z /Mir
Struggling to get my head around spaces/special characters in vb scripts/batch so any input appreciated.
Cheers
The main issue in your code is, that there are no quotes "" around the source and destination directories in the robocopy command line. Since they contain spaces, such are interpreted as argument separators; so %source%, which is expanded to the value C:\Program Files (x86)\Phoresis\Backup, will be interpreted as three arguments C:\Program, Files and (x86)\Phoresis\Backup. To avoid that, put "" around the path arguments like "%source%".
Here is the fixed code:
set "newDirectory=F:\Cap2 Flex Backup"
md "%newDirectory%"
rem Timesatmp a new folder
set "DirName=%newDirectory%\%date:~-4,4%.%date:~-7,2%.%date:~0,2%.%time:~0,2%.%time:~3,2%"
set "source=C:\Program Files (x86)\Phoresis\Backup"
robocopy "%source%" "%DirName%" /e /z /Mir
Additional issues/notes:
although you used quotes in your set command lines, which avoid troubles with some special characters (&, ^, (, ), SPACE, etc.), the "" are not part of the values stored in the variables; so when expanding (reading) the variables, you need to take care of such special characters once again, by surrounding the read variable by "";
the md command has been fixed (there was a leading \, but no opening ") and moved up;
variable DirName contains now also the parent (root) destination directory F:\Cap2 Flex Backup, which is stored in variable newDirectory, to avoid relative paths for robocopy; since source contains an absolute path too, the script can be located and executed anywhere;
regard that the timestamp retrieved by %date% and %time% depend on the locale and region settings of the system; therefore this script is not quite portable; see this post to overcome that;
Similar question to these have been tons of time but, for what i want it is something different. I have tried searching but, not what I want.
Now I have certain (.txt) files in my folder (hidden-files) where certain text files (Multiple text files) are stored which have special characters (%!=^";#% ..etc) in them I would like to remove these special characters and replace them with (-) in a rename loop so the file(s) will be renamed.
Code (Got it from here somewhere) Edited to my use but, it didn't worked.
for /f "delims=" %%a in ('dir "%~dp0hidden-files\*.txt"') do (set "newname=%~nx1"
set "newname=%newname:!=-%"
set "newname=%newname:_=-%"
set "newname=%newname:==-%"
set "newname=%newname:%=-%"
echo ren "%%a" "!newname!")
Would appreciate any help. Cheers.
You have a whole host of problems with your code. I'm too tired to list them all, but there is one issue that is extremely difficult to solve with pure batch - there is no simple way to replace an = literal within a string using batch. The simplest (and perhaps most effective) way to accomplish this is to look character by character for the = and replace it via substring operations.
There are a bunch of other issues that require advanced batch techniques to solve.
I have a much simpler solution for you - my JREN.BAT utility uses regular expression replacement to rename files. It is a hybrid JScript/batch script that runs natively on any Windows machine from XP onward. Use jren /? from the command line to access the built-in help. You might want to use jren /? | more to see one screen at a time. I never use MORE because my console is configured with a large buffer that lets me scroll up to see past output.
Once you have JREN.BAT on your machine, preferably in a folder that is within your PATH variable, then all you need is the following to rename the files within the current directory - no additional batch script required:
jren "[!_=%]" "-" /fm *.txt
If needed, you can specify the folder as an option:
jren "[!_=%]" "-" /fm *.txt /p "c:\YourPathHere"
If you put the command within another batch script, then you will need to use CALL JREN, and the percent needs to be double escaped as %%%%. Percent literals must be escaped as %% within batch files, and the CALL requires an extra round of escape:
#echo off
call jren "[!_=%%%%]" "-" /fm *.txt /p "%~dp0hidden-files"
Update in response to comment
Adding (, ), [, and ] to the list of characters to replace is simple. The only trick is you must escape the ] as \] when it appears within a character set [....].
#echo off
call jren "[!_=%%%%[\]()]" "-" /fm *.txt /p "%~dp0hidden-files"
The other option is to place the ] immediately after the opening [.
#echo off
call jren "[]!_=%%%%[()]" "-" /fm *.txt /p "%~dp0hidden-files"
Regular expressions are incredibly powerful, but they can be confusing to the uninitiated. Use jren /?regex to access Microsoft's help page on all the supported regular expression syntax. There are loads of tutorials available on the internet, as well as web sites that allow you to conveniently test out regular expressions.
If you are developing a JREN command, I suggest you first use the /t option to test it out without actually renaming anything.
Last update for question in comment
File names cannot contain *, ?, |, <, >, \, /, : or ", so you don't need to worry about those characters. I'm not sure what you did, but I don't see the enclosing [...] to represent a character set. The following should work fine:
call jren "[`~!##$%%%%^&_ +=,';}{[\]()]" "-" /fm *.txt /p "%~dp0hidden-files"
I have a batch script that eventually starts another batch file and waits for it to complete. Here is the syntax I originally had:
for %%i in ("*.xml") do start /separate /wait "%PROGRAM_PATH%" "%LOCAL_OUTGOING_PATH%\%%i"
What happened is that instead of opening the program that %PROGRAM_PATH% points to, it ended up launching Internet Explorer and showing the XML file specified by %%i. It was like as though it ignored the %PROGRAM_PATH% portion of the start command. I tried using %PROGRAM_NAME% without quotes that didn't work either. %PROGRAM_PATH%, by the way, points to "
"C:\DOS\copy.bat". So I ended up having to hard code the path in there like this:
for %%i in ("*.xml") do start /separate /wait C:\DOS\copy.bat "%LOCAL_OUTGOING_PATH%\%%i"
This made it finally work the way I wanted it to. But I want to be able to use a variable. Why doesn't that work?
The first START parameter enclosed in quotes is taken as the Window Title. If you want to give a parameter enclosed in quotes you MUST first provide a title, even an empty one:
for %%i in ("*.xml") do start "" /separate /wait "%PROGRAM_PATH%" "%LOCAL_OUTGOING_PATH%\%%i"
or
for %%i in ("*.xml") do start "Win Title" /separate /wait "%PROGRAM_PATH%" "%LOCAL_OUTGOING_PATH%\%%i"
Type START /? for further details.