The `/b` switch in Command Prompt is a hidden but powerful tool for copying files exactly as they are—without any text or data interpretation. When you need to ensure that every byte of a file is copied intact—whether it’s a configuration file, a system backup, or a binary executable—this method guarantees no corruption. You don’t need special software; just the right command, and you’ll avoid accidental text rewrites or encoding issues.
What You'll Need
- A Windows PC with Command Prompt or PowerShell access (Command Prompt is sufficient).
- The two files you want to copy:
- A source file (e.g., `C:\oldfile.dat`).
- A destination file (e.g., `C:\newfile.dat`), or an existing file to append binary data (e.g., `C:\existingfile.txt` in binary mode).
- Administrator rights if copying to protected system directories (e.g., `C:\Windows`).
- No external tools—this works only with native Command Prompt.
Step-by-Step
-
Open Command Prompt as Administrator:
Press
Win + X, then selectCommand Prompt (Admin)from the menu. This ensures you can access restricted directories. -
Navigate to your source file’s directory using the command:
cd "path\to\source\folder"Replace the path with the actual folder where the file resides (e.g.,cd "C:\Users\YourName\Downloads"). -
Use the `/b` flag to copy the file:
Type this command, then press
Enter:copy /b "sourcefile" "destinationfile"Examples:- If copying to a new file:
copy /b "C:\oldfile.dat" "C:\newfile.dat" - If appending to an existing file (e.g., a text file with `0x` padding):
copy /b "backup.bin" + "C:\existingfile.txt" "C:\existingfile.txt"
- If copying to a new file:
-
Verify the copy:
Use a hex editor or compare file sizes:
- In Command Prompt:
dir /b "destinationfile"to check size. - Use a tool like Notepad++ (with hex view) or HxD to inspect raw bytes.
- In Command Prompt:
If That Did Not Work
If the copy fails silently, check these steps:-
Ensure the source file exists and is accessible. Type
dirin the same directory to confirm. -
Verify the destination path is correct. If copying to a network drive, ensure you’re in the right drive (e.g.,
cd \\server\share). - Disable antivirus temporarily: Some security software blocks `/b` operations. Add the command to an exception list.
- Reboot Command Prompt and try again. Old file handles can cause issues.
-
Use PowerShell instead (same method works):
Copy-Item -Path "sourcefile" -ToFile "destinationfile" -Force.
Common Mistakes
- Forgetting `/b`: Without the switch, Command Prompt treats files as text, corrupting binary data. Always include it for exact copies.
-
Incorrect path syntax: Windows requires double backslashes or forward slashes in paths (e.g.,
C:/oldfile.datinstead ofC:\oldfile.dat). Use raw paths likeC:\path\to\fileor\\server\share\file.txt. -
Trying to overwrite text files: If you copy a binary file to a text file (e.g.,
C:\olddata.dattoC:\notes.txt), the destination is replaced entirely. Append binary data only to files marked as binary in the editor (or use a hex editor). - Ignoring permissions: Some system folders (e.g., `C:\Windows`) require admin rights. Run Command Prompt as admin if needed.
Tips
-
Backup before copying: Double-check the source file’s integrity before running the command. Use tools like
robocopyfor verification:robocopy "source" "destination" /Lto list only the files. -
Copy large files efficiently: For files >1GB, break them into chunks using tools like
xcopy /borrobocopywith/ZB(restartable mode). -
Use `copy /b` for network shares: If copying from a mapped network drive (e.g., \\server\share), ensure the path includes the drive letter (e.g.,
\\server\share\file.bin). - Avoid Windows Defender scan: If Command Prompt freezes during the operation, the file may be flagged as suspicious. Run in Safe Mode or exclude the folder from scans.
- Test on a small file first: Copy a 1KB binary file (e.g., an empty `.exe` file) to confirm the method works before handling critical data.
Troubleshooting
-
Command Prompt hangs or crashes: The file may be locked by another process. Close any programs using it (e.g., antivirus, IDEs) or use
attrib -R -H -S sourcefileto remove read-only attributes (requires admin). -
Destination file is corrupted:
- Check for typos in the path. Use absolute paths (e.g.,
C:\newfile.dat) for reliability. - If copying to a USB drive, ensure it’s properly ejected first to avoid partial writes.
- Check for typos in the path. Use absolute paths (e.g.,
- Windows Error 5: Access Denied: The destination directory lacks permissions. Run Command Prompt as admin and try again. Alternatively, use the destination’s full path to bypass permission checks (not recommended for system folders).
-
Filesystem errors: Corrupted disks can fail `/b`. Run
chkdsk C: /f(replace C:) to repair drive errors.
Frequently Asked Questions
Why can’t I copy a text file using `/b`?
The `/b` switch treats all files as raw binary data. To preserve text files exactly, use:
copy source.txt destination.txt /A: (Append mode) or ensure the destination is also treated as binary in your editor.
What’s the difference between `/b` and `copy`?
The standard copy command interprets files as text, automatically converting line endings and metadata. `/b` bypasses this, copying only the byte stream—critical for executables, images, or any file containing non-text data.
Can I use `/b` to merge two binary files?
Yes! Append the second file to the first using:
copy /b file1.bin + file2.bin output.bin
This works for combining chunks of data without losing integrity.
Why does my copy show 0 bytes after running the command?
This usually means:
- The source file doesn’t exist (check spelling or path).
- The destination already exists and is being overwritten (use
+for appending). - The file is corrupted or empty (use
dir sourcefileto verify).
Is `/b` safe for system files?
Yes, but verify the source file is intact first. For critical system files (e.g., `boot.ini`), use a known-good backup or tools like Windows’ ntbackup (deprecated) or third-party utilities like Robocopy. Avoid manual editing at the command line.
Can I use `/b` over a network share?
Yes, but ensure:
- You’re using the correct path (e.g.,
\\server\share\file.dat). - Network credentials are valid (use
net useif needed).