Check If a File Exists with a Filename Containing Spaces

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



Leave a reply



Submit