How to run a script without worrying about special characters?

Asked

Viewed 753 times

3

I made a batch for windows to install some programs and make settings automatically, it worked normally, but when doing a test using a folder that had space in its name it stopped working, put in the variable "" and returned to normal.

However I want to avoid any future problem and I made a folder with some characters that can give problem, the name of the folder was this way:

New - action';#@$%&()+-.,{}][~~~~

This is my last test:

SET mypath="%~dp0"
cd %mypath%

If I opened cmd accessing the folder as administrator or with normal user works normally, but if I use the explore double click works with normal user but not as administrator, so open as administrator the script immediately closes.

My script should run as administrator so it has become a problem, of course this problem only occurs in this folder with the name full of special characters.

How to run a script without worrying about special characters?

I made a video for you to see what’s going on.

https://youtu.be/8O_3vHqqTwg

  • 1

    Have you thought about using the Powershell?

  • No, because I want something that the person just open and ready, I put a validation in the script, if it opens with ordinary user appears a message to run as administrator, if there is some way to run the script only with powershell for me it is okay. Because if I access using cmd as administrator I will go to the folder and run the script too.

  • I believe only if you create a real executable instead of using .bat.

  • I’m researching how to make an installer, but still I wanted to know how to work with any folder using a batch, because I always have to create a batch for something, and with this answer besides helping to solve this problem since the only problem in the script is the way, it would help me a lot in future scripts.

  • I did some more tests, the content independent script (I put only the "pause" command), if it is inside the "New - action paste_action';#@$% &()+-folder. ,{}][~~~~~~" and if it runs as an administrator using windows explorer it opens nothing and closes.

  • I made a video for you to see what’s going on.

Show 1 more comment

1 answer

5


I found only one way to solve your problem. I did my script and it worked right here, but you can choose to do your

you will make a "shell" for your script with the program Bat To Convert EXE I use this technique a lot when I want to compile a script to be able to edit it in the future.

this is the script of the shell

@echo off
call "script.cmd"

you will compile it with the program and mark the option "add administrator manifest"

the shell will be with administrative privilege and will then call your script inward.

the script I made was this one.

@echo off
echo.>new_file.txt
ping 0 -n 2 >nul
FOR %%c in (new_file.txt) do (set cam="%%~dpc")
echo.%cam%>dir.txt
pause

setlocal enabledelayedexpansion
for /f "tokens=* delims= " %%F in (dir.txt) do (set linha=%%F
set linha=!linha:"=!
set mypath=!linha!
set mypath=!mypath:^(=^(!
set mypath=!mypath:^)=^)!
set mypath=!mypath:^¬=^¬!

set mypath=!mypath!
echo.!mypath!
cd !mypath!
pause
)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.