0

I need to move a file from one directory to another without any fixed paths in the copy/paste command. That means I likely cannot have a one-liner copy/paste command:

The folder structure is as follows:

  • Mainfolder
    • Files.dir
    • log.dir
    • executable.bat

This is my code:

cd log
if exist flist.log del flist.log
cd ..
cd Files
dir /b /a-d>flist.log
copy flist.log
cd ..
cd log
paste flist.log

Or, if possible have something like this:

cd log
if exist flist.log del flist.log
cd ..
cd Files
dir /b /a-d>flist.log (save in Files.dir without a fixed path)

I cannot use a fixed path, because the folder this is executed in, needs to be able to move.

Thanks :)

1
  • Files.dir? Are you trying to write a single batch script for Windows, Unix, and OpenVMS? Oct 21, 2018 at 13:47

2 Answers 2

0

You don't. Copy/paste of files generally does not exist in the command shell.

Instead you directly tell it where to move the file:

if exist log\flist.log del log\flist.log

cd Files
dir /b /a-d > flist.log

move flist.log ..\log\

Or, in fact, directly tell it where to write the file in the first place:

cd Files
dir /b /a-d > ..\log\flist.log

There is no basis for your "No oneliners" requirement. As you can see, none of the examples above use a fixed path; all commands accept relative paths (with .. or without) the same way as cd does.

1
  • This worked, Thank you! I looked for an answer to this, but i only found how to use xcopy and things like that. Mostly oneliners with fixed paths. It seemed like a simple thing to solve, but it was not easy finding an answer like yours :)
    – Tom
    Oct 21, 2018 at 14:13
0

It appears the intent of your script is to construct a list of files that are contained in the subdirectory files of your current working directory, the list being placed in subdirectory log which is alongside files.

Relative paths work, no need to be cd-ing all over the place.

dir /b /a-d files >log\flist.log

It it's intentional that you end up with two copies of the listing, which seems to be the case in your first example, then follow that up with

copy /y log\flist.log files\flist.log

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .