if statement comparing path with a space AutoHotKey - if-statement

Because of the space in the space I get this error :
Error: The following variable name contains an illegal character
C:\Users\Ko La\Desktop
How can I resolve this?
if (%A_ScriptDir%="C:\Users\Ko La\Desktop")
return
else{
MsgBox, 4, , Do you want to rename the files in %A_ScriptDir%? (Press YES or NO)
IfMsgBox No
return
else{
MsgBox Rename is done
return
}
}
I also get the same error with when I use the short name :
if (%A_ScriptDir%="C:\Users\KOLA~1\Desktop")

Explanation
Your problem is not the string, its the variable %A_ScriptDir%.
In the if it is already a expression so A_ScriptDir gets replaced with the path and because of the % it is evaluated a second time, which is of course not possible and throws the error.
Solution (tl;dr)
Remove the % in line 1
if (A_ScriptDir="C:\Users\Ko La\Desktop")

Without knowing which line is throwing the error, I assume you are saying the issue is due to the space in the directory name? If so, fire up your command line (run cmd.exe) and change to the users directory:
cd \Users
type in:
dir /x
And find the "short" name for the folder. It is probably:
KOLA~1
Replace your first line with:
if (%A_ScriptDir%="C:\Users\KOLA~1\Desktop")
[or whatever the "short name" for the directory is]

Related

How do I list files and directories those are not hidden in current directory using crystal language?

I wrote my own minimal version of "ls" command (Linux) using crystal language and here is my code:
require "dir"
require "file"
def main()
pwd = Dir.current
list_dir = Dir.children(pwd)
puts("[+] location: #{pwd}")
puts("------------------------------------------")
list_dir.each do |line|
check = File.file?(line)
if check == true
puts("[+] file : #{line}")
elsif check == false
puts("[+] directory: #{line}")
else
puts("[+] unknown : #{line}")
end
end
end
main
It works but it also listing all hidden files and directories (.files & .directories) too and I do not want to show those. I want the result more like "ls -l" command's result not like "ls -la".
So, what I need to implement to stop showing hidden files and directories?
There is nothing special about "hidden" files. It's just a convention to hide file names starting with a dot in some contexts by default. Dir.children does not follow that convention and expects the user to apply approriate filtering.
The recommended way to check if a file name starts with a dot is file_name.starts_with?(".").

Why am I getting a "The filename, directory name, or volume label syntax is incorrect." error?

I've been getting an error in this snippet of code that says my file path is incorrect. I know python likes to convert file paths to use double slashes (ie. \) however I am using a raw string variable. Does anyone understand why this is happening?
import os
comList = ['D:\\twidl\\data\\intel\\unlock\\unlock.bin\n', 'D:\\Kit025_02_TGF047K_7002\\BI\\TG-OEM\\Dell\\TGB047K_TGL051b7UB_1024.bin\n', 'D:\\twidl\\gui\\utils\\products.xml\n']
def remanTool():
for string in comList:
string1 = r"C:\Users\mgilmore\Desktop\FirmwareInstaller\WinPython-32bit-2.7.13.0Zero\python-2.7.13\python.exe"
string2 = r"C:\Users\mgilmore\Desktop\FirmwareInstaller\TWIDL\PSHH_Reman.py"
command = os.system(string1 + string2 + " -s " + comList[0] + " -f " + comList[1] + " -m " + comList[2] )
print command
I expect the command to be run, however it keeps saying the file path is wrong.
Also, for reference, comList is basically just an array of directories.
The directories for some reason have been saved with endlines and double slashes. I've already tried using os.path.abspath().
Here is my error message:
The filename, directory name, or volume label syntax is incorrect.
I figured out what was wrong guys, it was just an error in my text editor. All I had to do was restart the software.

Running EXE files in a folder sequence

I have a sequence of folders (planx 1 , planx 2 ,.......,planx 35) each folder contains an exe file triton.exe, I wrote the following code but it gives me a syntax error,apparently the format I wrote especially '+str(i)' is wrong.
I tried to correct that by adding " "before +str(i) but it reads the folder name (planx1) without space and surly there is no such folder.
what can I do to make it work ?
import sys, string, os
for i in range(1, 35):
os.chdir( 'E:\\project\\x\\CR 0\\planx'+str(i))
os.system( '"E:\\project\\x\\CR 0\\planx'+str(i)'\\triton.exe"')
print('done')
You are missing the + after the str(i) to add the third string as well:
'...\planx'+str(i)+'\\trit...'
# ^ this one
although you may want to use os.path.join instead of adding them together

listing directories having "-" character in directories name

I want to list the directories in current directory having "-" character in directories name. I used os.listdir(path). Its giving me error :
"WindowsError: [Error 123] The filename, directory name, or volume
label syntax is incorrect:"
Any help will be greatly appreciated
Use os.listdir to get directory contents and then filter using os.path.isdir to check if each item is a dir:
dirs_with_hyphen = []
for thing in os.listdir(os.getcwd()):
if os.path.isdir(thing) and '-' in thing:
dirs_with_hyphen.append(thing)
print dirs_with_hyphen # or return, etc.
And that can be shortened using list comprehension:
dirs_with_hyphen = [thing for thing in os.listdir(os.getcwd()) if os.path.isdir(thing) and '-' in thing]
I'm using os.getcwd but you could pass in any string that represents a folder.
If you're getting an error about the filename being incorrect, you're probably not escaping correctly or it's not pointing to the right folder (absolute vs relative path issue).
I did some testing and I managed to get your error. I don't know if this is what you did to get the error though since no example has been provided.
What I did though is give an invalid drive path. Not one that could be valid and doesn't exist, one that is always wrong eg.'C::\' or 'CC:\' just anything that's not 'C:\'. As for your question.
Path should generally look like this, prefixing with r to ignore backslash as escape character or double backslash.
import os
path = r"C:\Users\Steven\Documents\"
path = "C:\\Users\\Steven\\Documents\"
for file in os.listdir(path):
if os.path.isdir(path+file) and '-' in file:
print path + file
#List Comp
[path+file for file in os.listdir(path) if os.path.isdir(path+file) and '-' in file]

How to get a file to be used as input of the program that ends with special character in python

I have an output file from a code which its name will ends to "_x.txt" and I want to connect two codes which second code will use this file as an input and will add more data into it. Finally, it will ends into "blabla_x_f.txt"
I am trying to work it out as below, but seems it is not correct and I could not solve it. Please help:
inf = str(raw_input(*+"_x.txt"))
with open(inf+'_x.txt') as fin, open(inf+'_x_f.txt','w') as fout:
....(other operations)
The main problem is that the "blabla" part of the file could change to any thing every time and will be random strings, so the code needs to be flexible and just search for whatever ends with "_x.txt".
Have a look at Python's glob module:
import glob
files = glob.glob('*_x.txt')
gives you a list of all files ending in _x.txt. Continue with
for path in files:
newpath = path[:-4] + '_f.txt'
with open(path) as in:
with open(newpath, 'w') as out:
# do something