check if a file containing spaces exist in batch file in Windows 10
You need to put parentheses on the same lines as the command.
if exist "C:\Program Files\Manufacturer\" (
copy F:\Manufacturer\Manufacturer.exe "C:\Program Files\Manufacturer\Manufacturer.exe"
c:
cd "C:\Program Files\Manufacturer\"
) else (
copy F:\Manufacturer\Manufacturer.exe "C:\Program Files (x86)\Manufacturer\Manufacturer.exe"
c:
cd "C:\Program Files (x86)\Manufacturer\"
)
Find files with spaces in the bash
Use find command with a space between two wildcards. It will match files with single or multiple spaces. "find ." will find all files in current folder and all the sub-folders. "-type f" will only look for files and not folders.
find . -type f -name "* *"
EDIT
To replace the spaces with underscores, try this
find . -type f -name "* *" | while read file; do mv "$file" ${file// /_}; done
Shell script issue with directory and filenames containing spaces
Use find -print0 | xargs -0
to reliably handle file names with special characters in them, including spaces and newlines.
find /bishare/IRP_PROJECT/SFTP/ -type f -print0 |
xargs -0 ls -al > "$Temp_Path/File_Posted_$CURRENT_DATE.txt"
Alternatively, you can use find -exec
which runs the command of your choice on every file found.
find /bishare/IRP_PROJECT/SFTP/ -type f -exec ls -al {} + \
> "$Temp_Path/File_Posted_$CURRENT_DATE.txt"
In the specific case of ls -l
you could take this one step further and use the -ls
action.
find /bishare/IRP_PROJECT/SFTP/ -type f -ls > "$Temp_Path/File_Posted_$CURRENT_DATE.txt"
You should also get in the habit of quoting all variable expansions like you mentioned in your post.
Bash: If statement does not see the filename grab by loop
You need to (double) quote each reference of $f
so that bash knows the 'a' and 'file' are not separate objects, eg:
for f in *; do
if [ -f "${f}" ]; then
LINE=$(wc -l < "${f}")
WORD=$(wc -c < "${f}")
echo "${f}" ${LINE} ${WORD}
fi
done
Related Topics
One-Liner: Print All Lines Except the Last 3
Displaying or Redirecting a Shell's Job Control Messages
How to Merge Two Seperate - Yet Similar - Codebases into One Svn Rep
Automating Killall Then Killall Level 9
Recursively Rename Files to Ascii Standard
How to Modify Eip's Tracee Forked Procee
Gitlab Ssh Requests Password and Ignoring Ssh Keys
Can Someone Explain How This "Shellshock" Code Works in Shell
How to Overlap and Merge Multiple Audio Files Using Ffmpeg
How to Execute 32-Bit Code in 64-Bit Process by Doing Mode-Switching
How to Make a Bash String of Command with Redirect and Pipe
Can Libpcap Reassemble Tcp Segments
Error When Installing Opencv on Ubuntu 14.04
How to Set a Non-Standard Baudrate on a Serial Port Device on Linux