需将批量建文件夹移文件的.bat脚本转换为QNAP适用的.sh脚本
Convert Windows Batch to QNAP Shell Script
Here's the equivalent bash script for your QNAP device that replicates the functionality of your original batch file—creating a folder for each file (named after the file without its extension) and moving the file into that folder:
#!/bin/bash # Iterate over all items in the current directory for file in *; do # Only process regular files (skip directories) if [ -f "$file" ]; then # Extract the filename without the extension folder_name="${file%.*}" # Create the folder (suppress errors if it already exists) mkdir -p "$folder_name" 2>/dev/null # Move the file into the newly created folder mv "$file" "$folder_name/" fi done
Key Differences & Explanations:
- Shebang Line:
#!/bin/bashtells QNAP's Linux system to use the bash shell to run this script. - File Check:
if [ -f "$file" ]; thenensures we only process actual files, not directories (avoids accidentally moving folders into other folders, a potential issue with the original batch script). - Filename Extraction:
${file%.*}removes the shortest suffix starting with a dot from the filename—this gives us the folder name without the file extension, just like%%~nain your batch script. - Folder Creation:
mkdir -p "$folder_name" 2>/dev/nullcreates the folder, and-pensures it doesn't throw an error if the folder already exists (similar tomd ... 2>nulin batch). The2>/dev/nullsuppresses any unnecessary error messages. - File Move:
mv "$file" "$folder_name/"moves the file into the target folder, matching themovecommand in your original script.
How to Use This on QNAP:
- Open a text editor (like Notepad or TextEdit) and paste the script above.
- Save the file with a
.shextension (e.g.,organize_files.sh). - Transfer the file to your QNAP device via File Station or SSH.
- Make the script executable: run
chmod +x /path/to/organize_files.sh(replace/path/to/with the actual location of the script on your QNAP). - Run the script: navigate to the directory containing your files (using
cd /path/to/files), then execute./organize_files.sh.
Notes:
- This script processes all visible files in the directory where it's run. Double-check you're in the correct folder before executing.
- Hidden files (those starting with a
.) won't be processed by default. If you need to include them, modify the loop tofor file in .* *; doand add an extra check to skip.and..directories withif [ "$file" = "." ] || [ "$file" = ".." ]; then continue; fi.
内容的提问来源于stack exchange,提问作者Bryan
相关产品推荐
相关产品推荐

