Move all files (only files not subdirs) in current directory

Advertisement

andresp
Joined:
Posts:
4

Move all files (only files not subdirs) in current directory

I was hoping

mv *.* newDirectory/

would do it, but that also moves the directories in the current directory.

Is there any way to move only the files?

Thanks

Reply with quote

Advertisement

martin
Site Admin
martin avatar
Joined:
Posts:
40,476
Location:
Prague, Czechia

Re: Move all files (only files not subdirs) in current directory

It's not possible with scripting. But you can easily do this from PowerShell via WinSCP .NET assembly.

Reply with quote

frustrated
Guest

meh

Part of the issue is that sub-directories that don't match the filemask are moved

mv *.* /some/other/dir/

shouldn't move anything that doesn't have a period on the name.
sub-directories without a period in the name are moved.

This is also true for the get command.

frustrating

Reply with quote

MarkZZ
Guest

Re: Move all files (only files not subdirs) in current directory

martin wrote:

It's not possible with scripting. But you can easily do this from PowerShell via WinSCP .NET assembly.

What would be the correct way to use the powershell transferOptions? I have a script that I need to upload only the text files from a directory tree and dump them all into one folder on the destination server rather than mirror the directory tree. The following setup seems to send only text files but also mirrors the directory tree:

# Upload files
$transferOptions = New-Object WinSCP.TransferOptions
$transferOptions.TransferMode = [WinSCP.TransferMode]::Binary
$transferOptions.filemask = "*.txt"

$transferResult = $session.PutFiles("c:\test2\", "/Home/*", $False, $transferOptions)

Any advice?

Reply with quote

MarkZZZ
Guest

Re: Move all files (only files not subdirs) in current directory

MarkZZ wrote:

martin wrote:

It's not possible with scripting. But you can easily do this from PowerShell via WinSCP .NET assembly.

What would be the correct way to use the powershell transferOptions? I have a script that I need to upload only the text files from a directory tree and dump them all into one folder on the destination server rather than mirror the directory tree. The following setup seems to send only text files but also mirrors the directory tree:

# Upload files
$transferOptions = New-Object WinSCP.TransferOptions
$transferOptions.TransferMode = [WinSCP.TransferMode]::Binary
$transferOptions.filemask = "*.txt"

$transferResult = $session.PutFiles("c:\test2\", "/Home/*", $False, $transferOptions)

Any advice?

Ok so I figured out a solution to this problem - I had to create batch file that iterates through each of the subfolders and then calls a winscp.exe script to send only the files.

The contents of the batch file is as follows:
cd C:\test2
for /D /r %%i in (*) do (
cd %%i 
"C:\Program Files\winscp556\winscp.exe" /console /script="C:\Program Files\winscp556\UploadScript.txt"
)

and the contents of the upload script are basically:

option batch on
option confirm off
open user:password@sftp.somesite.com  -hostkey="ssh-rsa 2048 xx:xx:xx:xx:xx:xx"
option transfer binary
put -filemask="|*/" *.txt>24H
close
exit

If there is a better way (powershell preferred) I'd love to see the example code!

Thanks,

Mark

Reply with quote

Advertisement

martin
Site Admin
martin avatar
Joined:
Posts:
40,476
Location:
Prague, Czechia

Re: Move all files (only files not subdirs) in current directory

Your original question what about mv. So it seems that you have completely changed topic.

Your solution with iterating folders in a batch file is correct.
You can do the same in PowerShell. I do not have an example handy. Will consider creating one.

Regarding the *.* mask, see https://winscp.net/eng/docs/file_mask#exceptions

Reply with quote

Advertisement

You can post new topics in this forum