0

in my head this problem seems simple but I cant for the life of me figure it out. I want to use a function similar to os.replace() to move a file/folder from one location which could vary to one that is set whilst also preserving the name of it.

At this point I couldn't figure it out however to make it slightly more difficult I want to be able to drop a file onto the batch/python script and have the code detect the filepath for the file i dropped on it.

Sorry for the bad explanation in short:

import os
initialfilepath = "The filepath of the file i drop onto the batch/python file"
finalfilepath = "Predetermined/file/path etc"
os.replace(initialfilepath,finalfilepath) <--However i want to preserve the name of the file.

Any help would be greatly appreciated!

Compo
  • 36,585
  • 5
  • 27
  • 39
  • 1
    BY preserve do you just mean keep the same [basename](https://docs.python.org/3/library/os.path.html#os.path.basename) and change the parent directory? An by "detect the file path" do you mean determine the [absolute path](https://docs.python.org/3/library/os.path.html#os.path.basename) to the file? – joshmeranda Oct 13 '21 at 12:02
  • Your question mentions a batch file or a python file. This site helps you to fix a single specific and reproducible issue with your submitted code and information. You have not submitted any batch file or its content, or explained how it is failing to work as intended. Please revisit your question, and assigned tags, and use the [edit] button, to make the required improvements. – Compo Oct 13 '21 at 12:07
  • 1
    Batch file: `%1` Python: `sys.argv[1]` – Squashman Oct 13 '21 at 13:17
  • @joshmeranda I do indeed mean keep the same basename and change where it is stored, also by detecting the file path I mean so when moving the file instead of specifying the original file path i could store the original of any file placed upon the script in a variable and use that as a term in the moving process whatever that may be, I'm not the best at coding so apologies for the loose answers :) – Joe Wishman Oct 14 '21 at 18:12

1 Answers1

0

With a single line in batch file you can handle all your dropped files:

CMD /k FOR %%s in (%*) do ECHO %%s

In this example it will print all dropped files.
The parameter %* get all file pathnames.
The command FOR as it is shown, split the string by spaces, and handle one by one.
If the file pathname has spaces, so it will come in quotes as one thing.

Note that you don't need CMD /k, but it will keep the console opened at the end.
If you just wanna see before to close, insert PAUSE > nul at the end.

You can group the do commands between ( ... ) and then break the lines.
Consider set @ECHO off in beggining.

Now try it yourself :)
Type FOR /? to see more possibilities to manipulate parameters %~nX, %~dpX, %~xX...

Zano
  • 135
  • 9