You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

需将批量建文件夹移文件的.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/bash tells QNAP's Linux system to use the bash shell to run this script.
  • File Check: if [ -f "$file" ]; then ensures 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 %%~na in your batch script.
  • Folder Creation: mkdir -p "$folder_name" 2>/dev/null creates the folder, and -p ensures it doesn't throw an error if the folder already exists (similar to md ... 2>nul in batch). The 2>/dev/null suppresses any unnecessary error messages.
  • File Move: mv "$file" "$folder_name/" moves the file into the target folder, matching the move command in your original script.

How to Use This on QNAP:

  1. Open a text editor (like Notepad or TextEdit) and paste the script above.
  2. Save the file with a .sh extension (e.g., organize_files.sh).
  3. Transfer the file to your QNAP device via File Station or SSH.
  4. 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).
  5. 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 to for file in .* *; do and add an extra check to skip . and .. directories with if [ "$file" = "." ] || [ "$file" = ".." ]; then continue; fi.

内容的提问来源于stack exchange,提问作者Bryan

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.28 07:08:10