Solved Moving files with a bat-file


Samun

Member
Member
Local time
4:32 PM
Posts
37
OS
Windows 11
Hey guys,

a hopefully quick question. I need a batfile that is executed in a folder without using the path. I'd put it into a gazillion other folders later and execute it everywhere. So, in a given folder are two subfolders, A and B. All files from B should move to A. THat is all, but I cant get it done for the life of me.

Its this command (from a MS-page)


Examples​

To move all files with the .xls extension from the \Data directory to the \Second_Q\Reports directory, type:

move \data\*.xls \second_q\reports\



So, this is the correct syntax. What I do is this:

move \b\*.* \a\

The folders already exist, nothing is overwritten (they are testfolders anyway). The batfile is executed from the main-folder that contains the folders a and b. Please no other suggestions how to do this, this is about the syntax within this specific example, the actual problem is try to solve is way more complicated.

It just... doesnt... work... A window is opening, but closing immediately, so I cannot see an error. Where am I was being stupidly?
 

My Computer

System One

  • OS
    Windows 11
    Computer type
    PC/Desktop
    CPU
    Ryzen 5 2600x
    Motherboard
    X470 Gaming Plus
    Memory
    32GB DDR4
    Graphics Card(s)
    RTX 3060
    Sound Card
    My last one was Soundblaster 16
    Monitor(s) Displays
    2
    Screen Resolution
    2560x1440 both; 120Hz and 75Hz
    Hard Drives
    SSD M.2 2TB with System, and a HDD 8TB for stuff
First, so that you can see the error message without the window closing do this:

Rather than running the command by double clicking on the batch file open up a command prompt first. Then change directories to the location where the batch file is by typing in CD \<path_to_batch_file> and then hit ENTER. Then type in the name of the batch file and hit enter. Because the command prompt is already open it won't close on you when the batch file is run, and you will be able to see any messages that are output.

Secondly, I see that you are using paths such as \B and \A. The \ indicates the root of the drive. So, what you are actually specifying is a folder by the name of B or A located right off the root of the drive. You mentioned that you want to be able to run this batch file inside of any particular directory so you should use relative paths. As an example:

".\A" or ".\B". The period denotes the current directory so ".\A" Indicates a subfolder called "A" beneath the current directory.

You might also want to include "cd /d %~dp0" near the start of the batch file. That causes the batch file to change directories to the location from which the batch file is being run. Then other paths can be specified as relative paths to the current folder. So, putting it together, you end up with this:

Batch:
@echo off
cd /d %~dp0
move .\data\*.xls .\second_q\reports\

The result of this would be that any files with a .XLS extension in a subfolder called "data" will be moved to a subfolder called "second_q\reports".

If any of this is not clear or if you need further help with it, please do let me know.
 

My Computers

System One System Two

  • OS
    Win11 Pro 25H2 (RTM+)
    Computer type
    PC/Desktop
    Manufacturer/Model
    Acemagic
    CPU
    Intel i7-14650HX
    Memory
    32 GB
    Graphics Card(s)
    No GPU - Built-in Intel Graphics
    Sound Card
    Integrated
    Monitor(s) Displays
    Varies as machine will often be moved to locations with different monitors
    Screen Resolution
    Varies
    Hard Drives
    1 x 1TB Gen 4 NVMe SSD
    PSU
    120W Power Brick
    Keyboard
    Corsair K70 Max RGB Magnetic Keyboard
    Mouse
    Logitech MX Master 3
    Internet Speed
    1Gb Up / 1 Gb Down
    Browser
    Edge
    Antivirus
    Windows Defender
  • Operating System
    Win11 Pro 25H2 (RTM+)
    Computer type
    Laptop
    Manufacturer/Model
    Lenovo ThinkBook 13x Gen 2
    CPU
    Intel i7-1255U
    Memory
    16 GB
    Graphics card(s)
    Intel Iris Xe Graphics
    Sound Card
    Realtek® ALC3306-CG codec
    Monitor(s) Displays
    13.3-inch IPS Display
    Screen Resolution
    WQXGA (2560 x 1600)
    Hard Drives
    2 TB 4 x 4 NVMe SSD
    PSU
    USB-C / Thunderbolt 4 Power / Charging
    Keyboard
    Backlit, spill resistant keyboard
    Mouse
    Buttonless Glass Precision Touchpad
    Internet Speed
    1Gb Up / 1Gb Down
    Browser
    Edge
    Antivirus
    Windows Defender
    Other Info
    WiFi 6e / Bluetooth 5.1 / Facial Recognition / Fingerprint Sensor / ToF (Time of Flight) Human Presence Sensor
Run those commands within a cmd prompt and you'll probably see the problem better.
I do not think that the bits of syntax that you have posted can possibly be correct. Your cmd prompt test will confirm that.
Batch script is not designed with relative paths in mind. All I should have said is that I have experimented with it but do not have a single example of using it for real.
If you provided a link to the MS page you looked at, people might be able to suggest what you have misread on that page [if that's the case].

I am not at all clear what you are trying to do.
Do you want to stick the batch file wherever it suits for for each use and move something in a specific subfolder of wherever you have stuck the batch file on that occasion?
The subfolder names will always be the same?


Denis
 
Last edited:

My Computer

System One

  • OS
    Windows 11 Home x64 Version 25H2 Build 26200.8037
Thank you. So I missed a single "." ;)

But before I close the thread. I don't understand the cd /d %~dp0-part. I have a mainfolder with subfolders and even without that command I can copy files from one subfolder to the other. The "." seems already to give the command that the executing folder is the one the bat-file is in. So... what would be a case where you need "cd /d %~dp0"?

@Try3
That was the page. I am not really into reading stuff like this and I might have made a mistake.

But its all done already. I know what I have to do, thanks.
 

My Computer

System One

  • OS
    Windows 11
    Computer type
    PC/Desktop
    CPU
    Ryzen 5 2600x
    Motherboard
    X470 Gaming Plus
    Memory
    32GB DDR4
    Graphics Card(s)
    RTX 3060
    Sound Card
    My last one was Soundblaster 16
    Monitor(s) Displays
    2
    Screen Resolution
    2560x1440 both; 120Hz and 75Hz
    Hard Drives
    SSD M.2 2TB with System, and a HDD 8TB for stuff

My Computer

System One

  • OS
    Windows 11 Home x64 Version 25H2 Build 26200.8037
Batch script is not designed with relative paths in mind.
Batch files work perfectly with relative paths. I have entire programs written as nothing more than huge batch files referencing literally dozens of relative paths, all running flawlessly.
 

My Computers

System One System Two

  • OS
    Win11 Pro 25H2 (RTM+)
    Computer type
    PC/Desktop
    Manufacturer/Model
    Acemagic
    CPU
    Intel i7-14650HX
    Memory
    32 GB
    Graphics Card(s)
    No GPU - Built-in Intel Graphics
    Sound Card
    Integrated
    Monitor(s) Displays
    Varies as machine will often be moved to locations with different monitors
    Screen Resolution
    Varies
    Hard Drives
    1 x 1TB Gen 4 NVMe SSD
    PSU
    120W Power Brick
    Keyboard
    Corsair K70 Max RGB Magnetic Keyboard
    Mouse
    Logitech MX Master 3
    Internet Speed
    1Gb Up / 1 Gb Down
    Browser
    Edge
    Antivirus
    Windows Defender
  • Operating System
    Win11 Pro 25H2 (RTM+)
    Computer type
    Laptop
    Manufacturer/Model
    Lenovo ThinkBook 13x Gen 2
    CPU
    Intel i7-1255U
    Memory
    16 GB
    Graphics card(s)
    Intel Iris Xe Graphics
    Sound Card
    Realtek® ALC3306-CG codec
    Monitor(s) Displays
    13.3-inch IPS Display
    Screen Resolution
    WQXGA (2560 x 1600)
    Hard Drives
    2 TB 4 x 4 NVMe SSD
    PSU
    USB-C / Thunderbolt 4 Power / Charging
    Keyboard
    Backlit, spill resistant keyboard
    Mouse
    Buttonless Glass Precision Touchpad
    Internet Speed
    1Gb Up / 1Gb Down
    Browser
    Edge
    Antivirus
    Windows Defender
    Other Info
    WiFi 6e / Bluetooth 5.1 / Facial Recognition / Fingerprint Sensor / ToF (Time of Flight) Human Presence Sensor
Bat File.webp
 

My Computers

System One System Two

  • OS
    Win 11 Home ♦♦♦26200.8655 ♦♦♦♦♦♦♦25H2
    Computer type
    PC/Desktop
    Manufacturer/Model
    Built by Ghot® [May 2020]
    CPU
    AMD Ryzen 7 3700X
    Motherboard
    Asus Pro WS X570-ACE (BIOS 5302)
    Memory
    G.Skill (F4-3200C14D-16GTZKW)
    Graphics Card(s)
    EVGA RTX 2070 (08G-P4-2171-KR)
    Sound Card
    Realtek ALC1220P / ALC S1220A
    Monitor(s) Displays
    Dell U3011 30"
    Screen Resolution
    2560 x 1600
    Hard Drives
    2x Samsung 860 EVO 500GB,
    WD 4TB Black FZBX - SATA III,
    WD 8TB Black FZBX - SATA III,
    DRW-24B1ST CD/DVD Burner
    PSU
    PC Power & Cooling 750W Quad EPS12V
    Case
    Cooler Master ATCS 840 Tower
    Cooling
    CM Hyper 212 EVO (push/pull)
    Keyboard
    Ducky DK9008 Shine II Blue LED
    Mouse
    Logitech Optical M-100
    Internet Speed
    300/300
    Browser
    Firefox (latest)
    Antivirus
    Bitdefender Total Security
    Other Info
    Speakers: Klipsch Pro Media 2.1
  • Operating System
    Windows XP Pro 32bit w/SP3
    Computer type
    PC/Desktop
    Manufacturer/Model
    Built by Ghot® (not in use)
    CPU
    AMD Athlon 64 X2 5000+ (OC'd @ 3.2Ghz)
    Motherboard
    ASUS M2N32-SLI Deluxe Wireless Edition
    Memory
    TWIN2X2048-6400C4DHX (2 x 1GB, DDR2 800)
    Graphics card(s)
    EVGA 256-P2-N758-TR GeForce 8600GT SSC
    Sound Card
    Onboard
    Monitor(s) Displays
    ViewSonic G90FB Black 19" Professional (CRT)
    Screen Resolution
    up to 2048 x 1536
    Hard Drives
    WD 36GB 10,000rpm Raptor SATA
    Seagate 80GB 7200rpm SATA
    Lite-On LTR-52246S CD/RW
    Lite-On LH-18A1P CD/DVD Burner
    PSU
    PC Power & Cooling Silencer 750 Quad EPS12V
    Case
    Generic Beige case, 80mm fans
    Cooling
    ZALMAN 9500A 92mm CPU Cooler
    Keyboard
    Logitech Classic Keybooard 200
    Mouse
    Logitech Optical M-BT96a
    Internet Speed
    300/300
    Browser
    Firefox 3.x ??
    Antivirus
    Symantec (Norton)
    Other Info
    Still assembled, still runs. Haven't turned it on for 15 years?
Hey guys,

a hopefully quick question. I need a batfile that is executed in a folder without using the path. I'd put it into a gazillion other folders later and execute it everywhere. So, in a given folder are two subfolders, A and B. All files from B should move to A. THat is all, but I cant get it done for the life of me.

Its this command (from a MS-page)


Examples​

To move all files with the .xls extension from the \Data directory to the \Second_Q\Reports directory, type:

move \data\*.xls \second_q\reports\



So, this is the correct syntax. What I do is this:

move \b\*.* \a\

The folders already exist, nothing is overwritten (they are testfolders anyway). The batfile is executed from the main-folder that contains the folders a and b. Please no other suggestions how to do this, this is about the syntax within this specific example, the actual problem is try to solve is way more complicated.

It just... doesnt... work... A window is opening, but closing immediately, so I cannot see an error. Where am I was being stupidly?

A useful debugging technique is to put "pause" at the end to hold the window open.

Ben
 

My Computer

System One

  • OS
    Windows 10 Home
    Computer type
    Laptop
    Manufacturer/Model
    HP Elitebook 2530p
    CPU
    Intel Core 2 Duo [email protected] GHz
    Memory
    2 GB
I don't understand the cd /d %~dp0-part.

That command changes the drive and directory to the location in which the batch file resides. That way, when you reference a relative path it will be relative to the location of the batch file and not whatever location the batch file was referenced from.

Think of this as if you were manually entering commands. For example, say you manually ran this command:

dir

That would list the directory contents of the folder from where you ran the command. However, what if you wanted to list the contents of a specific folder such as D:\MyFolder? Then you could do either one of these:

dir D:\MyFolder

OR

D:
CD \MyFolder
dir

In a batch file, the cd /d %~dp0 is like running the first two commands in the 2nd example above without having to know the path to specify. If substitutes the location of the rive letter and the path where the batch file is physically located so that yu can run that batch file from anywhere without having to change the path (as you said you wanted to do).
 

My Computers

System One System Two

  • OS
    Win11 Pro 25H2 (RTM+)
    Computer type
    PC/Desktop
    Manufacturer/Model
    Acemagic
    CPU
    Intel i7-14650HX
    Memory
    32 GB
    Graphics Card(s)
    No GPU - Built-in Intel Graphics
    Sound Card
    Integrated
    Monitor(s) Displays
    Varies as machine will often be moved to locations with different monitors
    Screen Resolution
    Varies
    Hard Drives
    1 x 1TB Gen 4 NVMe SSD
    PSU
    120W Power Brick
    Keyboard
    Corsair K70 Max RGB Magnetic Keyboard
    Mouse
    Logitech MX Master 3
    Internet Speed
    1Gb Up / 1 Gb Down
    Browser
    Edge
    Antivirus
    Windows Defender
  • Operating System
    Win11 Pro 25H2 (RTM+)
    Computer type
    Laptop
    Manufacturer/Model
    Lenovo ThinkBook 13x Gen 2
    CPU
    Intel i7-1255U
    Memory
    16 GB
    Graphics card(s)
    Intel Iris Xe Graphics
    Sound Card
    Realtek® ALC3306-CG codec
    Monitor(s) Displays
    13.3-inch IPS Display
    Screen Resolution
    WQXGA (2560 x 1600)
    Hard Drives
    2 TB 4 x 4 NVMe SSD
    PSU
    USB-C / Thunderbolt 4 Power / Charging
    Keyboard
    Backlit, spill resistant keyboard
    Mouse
    Buttonless Glass Precision Touchpad
    Internet Speed
    1Gb Up / 1Gb Down
    Browser
    Edge
    Antivirus
    Windows Defender
    Other Info
    WiFi 6e / Bluetooth 5.1 / Facial Recognition / Fingerprint Sensor / ToF (Time of Flight) Human Presence Sensor
I am going to veer off topic just slightly here. When you are experimenting with commands to move and copy files my personal preference is to use robocopy. Robocopy has a switch that will simulate the action that you are asking it to perform without actually carrying it out. This is great for testing so that you don't accidentally move or delete files unintentionally. It will act as if it is carrying out the command and display output as if it was running the command without actually doing it. It's perfect for debugging and testing. I realize that the syntax of the robocopy command can be a little bit daunting at times but it's well worth learning.

Example:

robocopy D:\Source D:\Destination /MOVE

This command will move all files and subfolders from D:\Source to D:\Destination. Now, modify the command by just adding "/L" like this:

robocopy D:\Source D:\Destination /MOVE /L

The "/L" tells robocopy to SIMULATE the action so that you can see exactly what it would do IF you actually ran the command but without moving or copying anything. Great for testing and experimenting in complete safety!
 

My Computers

System One System Two

  • OS
    Win11 Pro 25H2 (RTM+)
    Computer type
    PC/Desktop
    Manufacturer/Model
    Acemagic
    CPU
    Intel i7-14650HX
    Memory
    32 GB
    Graphics Card(s)
    No GPU - Built-in Intel Graphics
    Sound Card
    Integrated
    Monitor(s) Displays
    Varies as machine will often be moved to locations with different monitors
    Screen Resolution
    Varies
    Hard Drives
    1 x 1TB Gen 4 NVMe SSD
    PSU
    120W Power Brick
    Keyboard
    Corsair K70 Max RGB Magnetic Keyboard
    Mouse
    Logitech MX Master 3
    Internet Speed
    1Gb Up / 1 Gb Down
    Browser
    Edge
    Antivirus
    Windows Defender
  • Operating System
    Win11 Pro 25H2 (RTM+)
    Computer type
    Laptop
    Manufacturer/Model
    Lenovo ThinkBook 13x Gen 2
    CPU
    Intel i7-1255U
    Memory
    16 GB
    Graphics card(s)
    Intel Iris Xe Graphics
    Sound Card
    Realtek® ALC3306-CG codec
    Monitor(s) Displays
    13.3-inch IPS Display
    Screen Resolution
    WQXGA (2560 x 1600)
    Hard Drives
    2 TB 4 x 4 NVMe SSD
    PSU
    USB-C / Thunderbolt 4 Power / Charging
    Keyboard
    Backlit, spill resistant keyboard
    Mouse
    Buttonless Glass Precision Touchpad
    Internet Speed
    1Gb Up / 1Gb Down
    Browser
    Edge
    Antivirus
    Windows Defender
    Other Info
    WiFi 6e / Bluetooth 5.1 / Facial Recognition / Fingerprint Sensor / ToF (Time of Flight) Human Presence Sensor
Interesting. I will bookmark this in case I need this.

Thank you all for the help. (closed)
 

My Computer

System One

  • OS
    Windows 11
    Computer type
    PC/Desktop
    CPU
    Ryzen 5 2600x
    Motherboard
    X470 Gaming Plus
    Memory
    32GB DDR4
    Graphics Card(s)
    RTX 3060
    Sound Card
    My last one was Soundblaster 16
    Monitor(s) Displays
    2
    Screen Resolution
    2560x1440 both; 120Hz and 75Hz
    Hard Drives
    SSD M.2 2TB with System, and a HDD 8TB for stuff
But, for future, reference SS64 generally provide better explanations & guidance than MSLearn
Very helpful link!
 

My Computers

System One System Two

  • OS
    Win11 Pro 25H2 (RTM+)
    Computer type
    PC/Desktop
    Manufacturer/Model
    Acemagic
    CPU
    Intel i7-14650HX
    Memory
    32 GB
    Graphics Card(s)
    No GPU - Built-in Intel Graphics
    Sound Card
    Integrated
    Monitor(s) Displays
    Varies as machine will often be moved to locations with different monitors
    Screen Resolution
    Varies
    Hard Drives
    1 x 1TB Gen 4 NVMe SSD
    PSU
    120W Power Brick
    Keyboard
    Corsair K70 Max RGB Magnetic Keyboard
    Mouse
    Logitech MX Master 3
    Internet Speed
    1Gb Up / 1 Gb Down
    Browser
    Edge
    Antivirus
    Windows Defender
  • Operating System
    Win11 Pro 25H2 (RTM+)
    Computer type
    Laptop
    Manufacturer/Model
    Lenovo ThinkBook 13x Gen 2
    CPU
    Intel i7-1255U
    Memory
    16 GB
    Graphics card(s)
    Intel Iris Xe Graphics
    Sound Card
    Realtek® ALC3306-CG codec
    Monitor(s) Displays
    13.3-inch IPS Display
    Screen Resolution
    WQXGA (2560 x 1600)
    Hard Drives
    2 TB 4 x 4 NVMe SSD
    PSU
    USB-C / Thunderbolt 4 Power / Charging
    Keyboard
    Backlit, spill resistant keyboard
    Mouse
    Buttonless Glass Precision Touchpad
    Internet Speed
    1Gb Up / 1Gb Down
    Browser
    Edge
    Antivirus
    Windows Defender
    Other Info
    WiFi 6e / Bluetooth 5.1 / Facial Recognition / Fingerprint Sensor / ToF (Time of Flight) Human Presence Sensor
FWIW, I like to copy files to the destination and insure they all got there before removing them from the source.
 

My Computers

System One System Two

  • OS
    Win 11 Pro 25H2, Build 26200.8655
    Computer type
    PC/Desktop
    Manufacturer/Model
    Home Brew
    CPU
    Intel Core i5 14500
    Motherboard
    Gigabyte B760M G P WIFI
    Memory
    64GB DDR4
    Graphics Card(s)
    GeForce RTX 4060
    Sound Card
    Chipset Realtek
    Monitor(s) Displays
    LG 45" Ultragear, Acer 24" 1080p
    Screen Resolution
    5120x1440, 1920x1080
    Hard Drives
    Crucial P310 2TB 2280 PCIe Gen4 3D NAND NVMe M.2 SSD (O/S)
    Silicon Power 2TB US75 NVMe PCIe Gen4 M.2 2280 SSD (backup)
    Crucial BX500 2TB 3D NAND (2nd backup)
    Seagate 4TB Ironwolf, rotating HDD archive files
    External off-line backup Drives: 2 NVMe 4TB drives in external enclosures
    PSU
    Thermaltake Toughpower GF3 750W
    Case
    LIAN LI LANCOOL 216 E-ATX PC Case
    Cooling
    Lots of fans!
    Keyboard
    Microsoft Comfort Curve 2000
    Mouse
    Logitech G305
    Internet Speed
    Verizon FiOS 1GB
    Browser
    Firefox
    Antivirus
    Malware Bytes & Windows Defender Security
  • Operating System
    Win 11 Pro 25H2, Build 26200.8524
    Computer type
    PC/Desktop
    Manufacturer/Model
    Home Brew
    CPU
    Intel Core i5 14400
    Motherboard
    Gigabyte B760M DS3H AX
    Memory
    32GB DDR5
    Graphics card(s)
    Intel 700 Embedded GPU
    Sound Card
    Realtek Embedded
    Monitor(s) Displays
    27" HP 1080p
    Screen Resolution
    1920x1080
    Hard Drives
    Crucial P310 2TB 2280 PCIe Gen4 eD NAND PCIe SSD
    Samsung EVO 990 2TB NVMe Gen4 SSD
    Samsung 2TB SATA SSD
    PSU
    Thermaltake Smart BM3 650W
    Case
    Okinos Micro ATX Case
    Cooling
    Fans
    Keyboard
    Microsoft Comfort Curve 2000
    Mouse
    Logitech G305
    Internet Speed
    Verizon FiOS 1GB
    Browser
    Firefox
    Antivirus
    Malware Bytes & Windows Defender Security

Latest Support Threads

Back
Top Bottom